You are here: Re: PHP5 newbie question (i hope) .. using inherited methods from a subclass « PHP Programming Language « IT news, forums, messages
Re: PHP5 newbie question (i hope) .. using inherited methods from a subclass

Posted by Jerry Stuckle on 11/30/07 01:51

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
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация