Reply to Re: Array property of object with __get and __set - is this a bug?

Your name:

Reply:


Posted by ZeldorBlat on 09/26/87 11:31

Not really a bug..more a difference in the way __get and __set are
handled. Remember that when you use these, something like:

$x = $obj->y

does something slightly different than what you expect. Consider the
following:

function makeArray() {
$r = array(1, 2, 3, 4);
return $r;
}

Could you then write something like this:

echo makeArray()[2];

No, because makeArray is a function, not a reference or variable. The
same thing happens when you use __get() and __set(). Although it looks
like regular property accessing and setting, it really isn't.

This is the same reason you can't use overloaded __get() and __set with
empty(), unset(), etc. (well, now you can overload those, too).

The simplest solution to your problem is to pull out the array before
you try and access or set it's element:

$article = new Article();
$article->title = "my title";
$authors = array("Mr. Smith");
$article->authors = $authors;
print $authors[0] . "<br>";

[Back to original 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

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