Re: Using setters/getters with array of objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Tommy,

I've found both approaches (using setter/getter) as
recommended/non-recommended in documentation so this will be a difficult
decision.    Unfortunately I'll not be able to take your way since the Email
class (simplified in the example) is going to be used in other classes as
well.


On Sun, Oct 18, 2009 at 1:24 PM, Tommy Pham <tommyhp2@xxxxxxxxx> wrote:

> ----- Original Message ----
> > From: mbneto <mbneto@xxxxxxxxx>
> > To: php-general@xxxxxxxxxxxxx
> > Sent: Sun, October 18, 2009 8:31:53 AM
> > Subject:  Using setters/getters with array of objects
> >
> > Hi,
> >
> > I have two classes User and Email where one User can have many Emails so
> > I've done like this
> >
> > class Email
> > {
> >     protected $_email;
> >
> >     public function __get($name)
> >     {
> >         $property = '_' . $name;
> >         return $this->$property;
> >     }
> >
> >     public function __set($name, $value)
> >     {
> >         $property = '_' . $name;
> >         $this->$property = $value;
> >     }
> > }
> >
> >
> > class User
> > {
> >     protected $_name;
> >     protected $_emails = array();
> >
> >     public function __get($name)
> >     {
> >         $property = '_' . $name;
> >         return $this->$property;
> >     }
> >
> >     public function __set($name, $value)
> >     {
> >         $property = '_' . $name;
> >         $this->$property = $value;
> >     }
> >
> > }
> >
> > So I'd like to
> >
> > $u = new User();
> > $u->name = 'xxxx';
> >
> > $e = new Email();
> > $e->email = 'xxx@xxxxxxxx';
> >
> > $u->emails[] = $e;
> >
> > But that does not work.  I've managed to achieve similar result using a
> > different setter in User
>
> Of course it doesn't work because you didn't have 'set' method for the
> protected $_emails.
> http://www.php.net/manual/en/language.oop5.visibility.php
>
> >
> >     public function __set($name, $value)
> >     {
> >         $property = '_' . $name;
> >
> >         switch($name)
> >         {
> >             case 'emails':
> >                 array_push($this->$property, $value);
> >                 break;
> >
> >             default:
> >                $this->$property = $value;
> >         }
> >     }
> >
> > And then
> >
> > $u = new User();
> > $u->name = 'xxxx';
> >
> > $e = new Email();
> > $e->email = 'xxx@xxxxxxxx';
> >
> > $u->emails = $e;
> >
> > But this can confuse the programmer.  Any ideas of why it is not working?
>
> I suggest you don't use magic methods as it's too ambiguous and hard to
> expand your code later.  Your 2 classes could be summarized as 1 class
> below:
>
> class User
> {
>    protected $_name;
>    protected $_emails = array();
>
>     public function getName()
>    {
>        return $this->_name;
>    }
>
>    public function setName($value)
>    {
>        $this->_name = $value;
>    }
>
>    public function getEmails() {
>        return $this->_emails();
>    }
>
>    public function setEmails($arrayList) {
>      $this->_emails = $arrayList;
>    }
>
>   public function setEmail($name, $value) {
>       $this->_emails[$name] = $value;
>   }
>
>   public fuction getEmail($name) {
>      if (isset($this->_emails[$name]))
>         return $this->_emails[$name];
>      else
>         return null;
>   }
> }
>
> $u = new User();
> $u->setName('jon doe');
> $u->setEmail('email1', 'jon@xxxxxxxxx');
>
> Regards,
> Tommy
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux