|
Posted by ZeldorBlat on 11/30/07 01:51
On Nov 29, 8:22 pm, bizt <bissa...@yahoo.co.uk> wrote:
> 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
What if you declare $newsID, $title, etc. as protected instead of
private. Private visibility means that the method or property is only
available from that class. Protected visibility means that it's
available from anywhere in the inheritance hierarchy.
This code demonstrates that the superclass can access protected
members of a subclass:
class Superclass {
public function foo() {
echo $this->bar;
}
}
class Subclass extends Superclass {
protected $bar = 'bar';
public function bar() {
$this->foo();
}
}
$c = new Subclass();
$c->bar();
If I'm misunderstanding your problem let me know.
[Back to original message]
|