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

Your name:

Reply:


Posted by Wayne on 10/20/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,

[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

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