Using setters/getters with array of objects
Using setters/getters with array of objects
am 18.10.2009 17:31:53 von mbneto
--0023545bd6581b7ab104763754fb
Content-Type: text/plain; charset=ISO-8859-1
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@xxxx.com';
$u->emails[] = $e;
But that does not work. I've managed to achieve similar result using a
different setter in User
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@xxxx.com';
$u->emails = $e;
But this can confuse the programmer. Any ideas of why it is not working?
--0023545bd6581b7ab104763754fb--
Re: Using setters/getters with array of objects
am 18.10.2009 19:00:46 von andy-lists
--Apple-Mail-1-552655613
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii;
format=flowed;
delsp=yes
Hi,
>
> $u->emails[] = $e;
I would hazard a guess because $u->emails isn't a concrete object
(whereas $u->_emails is, but is private.) It's sort of a virtual
reference - PHP has no way of knowing that $u->emails actually
translates into _emails which is an array, if you see what I mean
(it's difficult to explain.)
>
> But that does not work. I've managed to achieve similar result
> using a
> different setter in User
>
> public function __set($name, $value)
> {
> $property = '_' . $name;
>
> switch($name)
> {
> case 'emails':
> array_push($this->$property, $value);
> break;
>
> default:
> $this->$property = $value;
> }
> }
You could also have done:
if (is_array($this->$property))
{
array_push($this->$property, $value);
}
else
{
$this->$property = $value;
}
which would handle any array property, not just the e-mails property.
If this was me, I would probably create a concrete method, called
"addEmail" which would do $this->_emails[] = $value, but allow a
programmer to call $user->emails to get the e-mails (not set.)
--Apple-Mail-1-552655613--
Re: Using setters/getters with array of objects
am 18.10.2009 19:24:04 von Tommy Pham
----- Original Message ----
> From: mbneto
> To: php-general@lists.php.net
> Sent: Sun, October 18, 2009 8:31:53 AM
> Subject: [PHP] 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@xxxx.com';
>
> $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@xxxx.com';
>
> $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@inter.net');
Regards,
Tommy
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Using setters/getters with array of objects
am 20.10.2009 21:22:09 von mbneto
--0023545bda285616ea047662c79b
Content-Type: text/plain; charset=ISO-8859-1
Hi,
Thanks. I'll probably do the addEmail method. I was hoping to do as with
the other "non-array" properties.
On Sun, Oct 18, 2009 at 1:00 PM, Andy Shellam (Mailing Lists)
wrote:
> Hi,
>
>
> $u->emails[] = $e;
>
>
> I would hazard a guess because $u->emails isn't a concrete object (whereas
> $u->_emails is, but is private.) It's sort of a virtual reference - PHP has
> no way of knowing that $u->emails actually translates into _emails which is
> an array, if you see what I mean (it's difficult to explain.)
>
>
> But that does not work. I've managed to achieve similar result using a
> different setter in User
>
> public function __set($name, $value)
> {
> $property = '_' . $name;
>
> switch($name)
> {
> case 'emails':
> array_push($this->$property, $value);
> break;
>
> default:
> $this->$property = $value;
> }
> }
>
>
> You could also have done:
>
> if (is_array($this->$property))
> {
> array_push($this->$property, $value);
> }
> else
> {
> $this->$property = $value;
> }
>
> which would handle any array property, not just the e-mails property.
>
> If this was me, I would probably create a concrete method, called
> "addEmail" which would do $this->_emails[] = $value, but allow a programmer
> to call $user->emails to get the e-mails (not set.)
>
>
--0023545bda285616ea047662c79b--
Re: Using setters/getters with array of objects
am 20.10.2009 21:38:09 von mbneto
--0015174481da8eccd70476630094
Content-Type: text/plain; charset=ISO-8859-1
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 wrote:
> ----- Original Message ----
> > From: mbneto
> > To: php-general@lists.php.net
> > Sent: Sun, October 18, 2009 8:31:53 AM
> > Subject: [PHP] 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@xxxx.com';
> >
> > $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@xxxx.com';
> >
> > $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@inter.net');
>
> Regards,
> Tommy
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--0015174481da8eccd70476630094--