SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 60
Thread: How useful is a Dataspace?
-
Jan 6, 2006, 23:16 #1
- Join Date
- Oct 2004
- Location
- naperville
- Posts
- 189
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How useful is a Dataspace?
What are the pros and cons of a dataspace (think the skeleton threads)? My initial gut reaction tells me its unnescesarry, but the wide spread use indicates I'm missing something. PHP5, if it matters.
What should a dataspace do?
Originally Posted by selkirk
Last edited by Super Phil; Jan 10, 2006 at 00:37.
-
Jan 7, 2006, 05:17 #2
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Super Phil
-
Jan 7, 2006, 08:52 #3
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:interface IDataspace {
public function set();
public function get();
public function has();
}
abstract class Dataspace implements IDataspace {
protected $parameters = array();
public function __construct() {}
public function set() {
$parameters = func_get_args();
if( is_array( $parameters ) ) {
$this -> parameters[strtolower( array_shift( $parameters ) )] = array_shift( $parameters );
}
}
public function get() {
$parameters = func_get_args();
if( is_array( $parameters ) ) {
if( array_key_exists( $parameter = array_shift( $parameters ), $this -> parameters ) ) {
return $this -> parameters[$parameter];
}
}
return false;
}
public function has() {
$parameters = func_get_args();
if( is_array( $parameters ) ) {
if( array_key_exists( $parameter = array_shift( $parameters ), $this -> parameters ) && !empty( $this -> parameters[$parameter] ) ) {
return true;
}
}
return false;
}
public function import( IDataspace $dataspace ) {
// commented out following, use the replacement below
// array_merge( $this -> parameters, $dataspace -> export() ); // old
// replacement...
$this -> parameters = array_merge( $this -> parameters, $dataspace -> export() ); // new
}
public function export() {
return $this -> parameters;
}
}
Last edited by Dr Livingston; Feb 25, 2006 at 13:40. Reason: fix a bug...
-
Jan 7, 2006, 11:26 #4
- Join Date
- Dec 2004
- Location
- ljubljana, slovenia
- Posts
- 684
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I agree with what Ezku and Dr Livingston said, I just have a small comment about import() and export() methods Dr Livingston introduces in his code. In a way, I feel import should be an inverse function of export, so either both should operate on array or both should operate on IDataspace. I usually use arrays with these and provide another method such as copyFrom(IDataspace $ds) (or copyTo) for importing data from another IDataspace.
-
Jan 7, 2006, 11:47 #5
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Super Phil
Dr Livingston shows what I consider the cleanest current PHP implementation. Things may change with PHP6 regarding property support, but I think the straightforward implementation above is best right now. It many people and frameworks standardized on it we would all be better off in my opinion because of the interoperablity that would occur.Christopher
-
Jan 7, 2006, 12:17 #6
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
For comparison, I use something along the lines of this.
PHP Code:class Container implements Iterator, ArrayAccess
{
/**
* @var array container data storage
*/
private $__data = array();
/**
* @param string resource name
* @return mixed reference to a resource, NULL if not found
*/
public function & get($name)
{
$retval = NULL;
if (isset($this->__data[$name]))
{
$retval =& $this->__data[$name];
}
return $retval;
}
/**
* @param string resource name
* @param mixed resource
* @return void
*/
public function set($name, $value)
{
$this->__data[$name] = $value;
return $value;
}
/**
* @param string resource name
* @return boolean
*/
public function has($name)
{
return isset($this->__data[$name]);
}
/**
* @param string resource name
* @return mixed resource value prior to removal, NULL if not found
*/
public function remove($name)
{
$retval = NULL;
if (isset($this->__data[$name]))
{
$retval = $this->__data[$name];
unset($this->__data[$name]);
}
return $retval;
}
/**
* Retrieve list of contained resources
* @return array
*/
public function names()
{
return array_keys($this->__data);
}
/**
* Fetch contained resources to an array
* @return array
*/
public function to_array()
{
return $this->__data;
}
/**
* Pick values from Container
* @param array list of picked value names, or alternatively a transform table (from => to)
* @return array
*/
public function pick($keys = array())
{
$retval = array();
foreach($keys as $to => $from)
{
$to = is_int($to) ? $from : $to;
$retval[$to] = $source[$from];
}
return $retval;
}
/**
* Merge current data with that from another array or Container
* @param mixed array or Container
* @return boolean false on failed merge
*/
public function merge($data)
{
switch (true)
{
case ($data instanceof self):
$data = $data->to_array();
case (is_array($data)):
$this->__data = array_merge($this->__data, $data);
return true;
default:
return false;
}
}
/**
* Container constructor. Pass a mergeable argument to set initial values.
* @param mixed initial values, optional
*/
public function __construct($data = NULL)
{
$this->merge($data);
}
public function __set($name, $value) { return $this->set($name, $value); }
public function & __get($name) { $val =& $this->get($name); return $val; }
// Implement Iterator
public function current() { return current($this->__data); }
public function key() { return key($this->__data); }
public function next() { return next($this->__data); }
public function rewind() { return reset($this->__data); }
public function valid() { return current($this->__data) !== FALSE; }
// Implement ArrayAccess
public function offsetExists($offset) { return isset($this->__data[$offset]); }
public function offsetSet($offset, $value) { return $this->set($offset, $value); }
public function offsetGet($offset) { return $this->get($offset); }
public function offsetUnset($offset) { unset($this->__data[$offset]); }
}
-
Jan 7, 2006, 12:38 #7
- Join Date
- Dec 2005
- Posts
- 29
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$parameters = func_get_args();
if( is_array( $parameters ) ) {
-
Jan 7, 2006, 12:46 #8
Is there any reason why the PHP5 implementations of DataSpace don't inherit from ArrayObject ?
-
Jan 7, 2006, 12:52 #9
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is always true.
-
Jan 7, 2006, 12:58 #10
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Ren
Christopher
-
Jan 7, 2006, 13:05 #11
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Ren
-
Jan 7, 2006, 14:25 #12
Originally Posted by Ezku
-
Jan 7, 2006, 15:29 #13
- Join Date
- Nov 2002
- Posts
- 841
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Think of a dataspace as a pattern for objects to declare properties with accessor methods and a standard interface for accessing the properties of an object. Properties form the data access facet of a component model.
Betrand Meyer (Object-oriented software construction, p 57) offers the "Uniform Access Principle:"
All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation.
Compare to C# properties, Java Beans (chapter 7), Objective C Key Value Coding, Ruby Attributes, Python Properties, Delphi Properties.
PHP is sadly deficient in this area compared to the languages it competes against.
I think I coined the term DataSpace in WACT. Its a terrible name and my understanding of this pattern has evolved much since then. I now prefer to say that a class implements the Properties interface, or implements the properties pattern. This means that an object implements a uniform notation for accessing data members which hides whether that data member is implemented via storage or computation.
There is an orthogonal issue as well. Sometimes it can be useful to bind to a component using an expression. You see this pattern repeated in the JSTL expression language, cocoa bindings, .Net data binding expressions and OGNL. Early in WACT the implementation of the UAP and the implementation of binding were coupled in the DataSpace class.
After the long and useful interface standards thread, I've come to see UAP support and binding support as separate concerns.
I dislike the name container because I think that name focuses too much on one possible implementation, and less on the intent.
I also dislike the name Keyed because I think that name focuses too much on the binding aspect.
Properties, Properties, Properties.
-
Jan 7, 2006, 16:27 #14
- Join Date
- Dec 2005
- Posts
- 29
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
So if I'm interpreting this correctly, you're suggesting that all classes should extend or implement the IDataSpace (or Properties) class for php5's magic funcs to implement (or something).
This would then make it seem more like what has been reffered to elsewhere as the common object.
Also, wouldn't the defined constant 'WACT_ROOT' then break this principle ? For example, suppose I have an application that wanted to be made available in the wact framework, my registry's (config) datasource would then have to figure out whether WACT_ROOT is in the main wact dataspace (config) or is a predefined constant but this seems like that should be the responsibility should the wact dataspace/datasource/config object, e.g $$wact->get('WACT_ROOT') ?
-
Jan 7, 2006, 19:50 #15
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Selkirk
There is a fundamental difference between $obj->name = $value and $obj->setname($value) and $obj->set('name', $value) that transcends functionality (because they do the same thing) and is about style and whether the things that the object deals with are other objects or arrays. Something like C# getters and setters would change things. But when we have things like $obj->$name = $value vs $obj->set$name($value) vs $obj->set($name, $value) I tend to resort to the last because it seem clearer in PHP.Christopher
-
Jan 7, 2006, 22:15 #16
I'm curious, but what do you guys consider to be the difference between a DataSpace/Container/Whathaveyou and a Model? That the latter is persistable? Or that it has methods to manipulate the data?
-
Jan 7, 2006, 22:25 #17
- Join Date
- Oct 2004
- Location
- naperville
- Posts
- 189
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What about some people who oppose it; ie, those who think that using class members and arrays is generally enough? (I'm not sure who this is, if anyone - marcus or mcgruff, do you guys have different opinions?). It's hard to make an educated opinion with just supporters.
33deg - as I understand it, dataspace's are purely for holding data. There would be no business logic, so its not a model (right?)
Is a dataspace + iterator a likely (the likely) use scenario? To play devils advocate, does it really have benfits past an array? I mean, arrays are reusable, etc
-
Jan 7, 2006, 22:33 #18
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think the benefit of a DataSpace is that it's an interface, thus it could be just an objectified array, but it might also be something more complex (such as a domainmodel object)
-
Jan 8, 2006, 08:11 #19
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Selkirk
-
Jan 8, 2006, 13:29 #20
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by kyberfabrikken
An example I can think of is the Response. It can be as simple as a straight PHP page, or as complex as a tree of Response Children each with Views that contain Templates. I have found that if Templates, Views and Response Children all implement DataSpace plus a render function then I can have as complex or as simple a Response as is needed by the page.Christopher
-
Jan 8, 2006, 14:11 #21
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Once you settle on some solid interfaces then many things start to fall into place because you gain flexiblity.
Interferance that would be there without the functionality of Interfaces that PHP5 introduces to us.
-
Jan 8, 2006, 14:18 #22
- Join Date
- Apr 2003
- Location
- London
- Posts
- 2,423
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Hi...
Originally Posted by Selkirk
yours, MarcusMarcus Baker
Testing: SimpleTest, Cgreen, Fakemail
Other: Phemto dependency injector
Books: PHP in Action, 97 things
-
Jan 8, 2006, 14:21 #23
- Join Date
- Apr 2003
- Location
- London
- Posts
- 2,423
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Hi...
Originally Posted by devosc
Of course if PHP has overloading of the [] accessor, all of this properties stuff would disappear.
yours, MarcusMarcus Baker
Testing: SimpleTest, Cgreen, Fakemail
Other: Phemto dependency injector
Books: PHP in Action, 97 things
-
Jan 8, 2006, 14:40 #24
- Join Date
- Apr 2003
- Location
- London
- Posts
- 2,423
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Hi...
Originally Posted by 33degrees
If you mean "domain model" or "business logic", again some of it may be persistent and some may not. You try to make it pure OO though, and reserve these types of interface for infrastructure stuff.
If you mean the "data model", then yeah, you'd have a common class that wouldn't really be a free and easy class, more of a direct mapping to the underlying system.
yours, MarcusMarcus Baker
Testing: SimpleTest, Cgreen, Fakemail
Other: Phemto dependency injector
Books: PHP in Action, 97 things
-
Jan 8, 2006, 15:18 #25
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lastcraft
Originally Posted by lastcraft
Christopher
Bookmarks