|
Posted by bizt on 11/30/07 01:22
Hi,
Im trying to use a value object class (where a single entity, perhaps
from a db, may be represented as an object), for example:
class News {
private $newsID;
private $title;
private $description;
private $dateAdded;
}
Now, instead of having to write accessor methods (ie. getNewsID) for
each property I am instead using __get and __set to check if an
accessor method exists. If one does, use the accessor method otherwise
return the value of the private property. See:
class News {
private $newsID;
private $title;
private $description;
private $dateAdded;
public function __get ($propertyName)
{
if (method_exists ($this, 'get'.$propertyName)) {
return call_user_func (array ($this, 'get'.$propertyName));
} else {
return $this->$propertyName;
}
}
public function __set ($propertyName, $value)
{
if (method_exists ($this, 'set'.$propertyName)) {
return call_user_func (array ($this, 'set'.$propertyName),
$value);
} else {
$this->$propertyName = $value;
}
}
}
The above example shows that all properties, altho private, can be
treated as public until the time comes that an accessor method is
required then on detecting that the accessor method exists, it will
refer to that instead. Ofcourse I must stick to the naming convention
of 'get[propertyName]' and 'set[propertyName]' which isnt really a
problem. Well, thats the theory and so far the above example is
working fine but I want to do something slightly different and maybe
my understanding of php classes is a little behind.
I have a few value objects for various tables and the code for each
__get and __set would be the same for each. So, this makes me think
that I need to create a parent ValueObject class containing the
overload methods (__get __set) and the value object classes (eg. News
and the rest) would inherit from ValueObject becoming subclasses of
it. See:
class ValueObject {
public function __get ($propertyName)
{
if (method_exists ($this, 'get'.$propertyName)) {
return call_user_func (array ($this, 'get'.$propertyName));
} else {
return $this->$propertyName;
}
}
public function __set ($propertyName, $value)
{
if (method_exists ($this, 'set'.$propertyName)) {
return call_user_func (array ($this, 'set'.$propertyName),
$value);
} else {
$this->$propertyName = $value;
}
}
}
class News extends ValueObject {
private $newsID;
private $title;
private $description;
private $dateAdded;
}
class Individual extends ValueObject { // another value object
..
..
..
This would be ideal as I obviously dont need to keep redeclaring the
__get and __set in each VO. However, I get an error when running it
this way because $this->$propertyName (well, onscreen it way say News::
$newsID if I try to access newsID property) does not exist this time.
Is that because it is trying to locate the property in the the
ValueObject class? I want each ValueObject subclasses to own the
method as though it were writing within its own brackets. Can this be
done easily? I have to confess I am new to PHP5 classes so Im hoping
that a more experienced PHP programmer may be able to spot where Im
going wrong. If someone could point me in the correct direction it
would be much appreciated. Thanks
Burnsy
[Back to original message]
|