PHP5 newbie question (i hope) .. using inherited methods from a
PHP5 newbie question (i hope) .. using inherited methods from a
am 30.11.2007 02:22:59 von bizt
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
Re: PHP5 newbie question (i hope) .. using inherited methods froma subclass
am 30.11.2007 02:51:16 von Jerry Stuckle
bizt 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
>
First of all, don't use __get or __set. They are a violation of OO
design concepts. Personally, I wish they had never been included in
PHP. They encourage poor coding such as this.
Rather, you should have get and set methods for each property, as
appropriate.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: PHP5 newbie question (i hope) .. using inherited methods from a
am 30.11.2007 02:51:53 von zeldorblat
On Nov 29, 8:22 pm, bizt 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.
Re: PHP5 newbie question (i hope) .. using inherited methods froma subclass
am 30.11.2007 03:00:11 von Jerry Stuckle
ZeldorBlat wrote:
> On Nov 29, 8:22 pm, bizt 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.
>
Which is a severe violation of OO principles. What happens, for
instance, if you do:
$c = new Superclass();
$c->foo();
Making Superclass::foo() protected only hides the problem. In true OO
languages, the code in Superclass::foo() would not be allowed (as it
shouldn't be).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: PHP5 newbie question (i hope) .. using inherited methods from a
am 30.11.2007 13:41:34 von bizt
> 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.
Yeh that seems to work fine thanks. I thought protected was declared
in the parent class but when inherited it would then be available to
the subclass. I need to properly understand visibility better i think,
I used objects in php4 and i done a little oop when at uni but this is
the first time ive really started to seriously start using proper oo.