|
Posted by Wayne on 10/06/20 11:31
On Mon, 7 Nov 2005 10:48:57 -0000, "Jon Maz"
<jonmaz@surfeu.no.spam.de> wrote:
>Hi All,
>
>I'm reasonably proficient with C#, and am starting with php5. I was happy
>to learn of the __get and __set methods, but am encountering a problem using
>them to set an object property of type "array".
>
>Below is a simple example of the problem I'm having with a sample object
>"Article", which has an array property "authors". As you can see, I can't
>even use standard php array syntax to set a single author, even though the
>same syntax outside of the object works perfectly.
>
>Am I doing something wrong here, or is this a php5 bug?
The problem is that arrays are values in PHP. So when you return an
array from a function (or assign it to variable) you're really
returing/assigning a copy of the array.
>//test with array property
>$article = new Article();
>$article->title = "my title";
>$article->authors[] = "Mr Smith";
This line adds "Mr Smith" to a copy of the authors array (as returned
by the __get function) and not the original array.
Your best bet is to use an ArrayObject rather than an array. It works
just like an array but, like all objects in PHP5, it's passed by
reference and should work as you expect.
Later,
Navigation:
[Reply to this message]
|