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