|
Posted by news.freeserve.com on 10/01/34 11:31
Hi All,
Thanks for the responses, they've helped me understand what's going on.
-------------
I think Oli hit the nail on the head when he wrote:
> > Could you then write something like this:
> > echo makeArray()[2];
> > No, because makeArray is a function, not a reference or variable.
> Actually, this is a limitation of PHP. In C, C++, C# and Java, the
> equivalent of the above code works fine.
I was indeed expecting PHP to work just like C#. Does anybody know if
there are plans to change this PHP behavior? Is this regarded as a bug
or a feature? (My vote is for bug).
-------------
In the end I have decided to solve the problem by replacing my properties
of type array() with properties of type CollectionBase, as found
here: http://coding.mu/archives/2003/08/09/php-collection-class/. I think
this is the solution Wayne was suggesting when he proposed using an
object instead of an array.
I decided to switch to this Collection object before Wayne gave the
ArrayObject tip. I'd never heard of ArrayObject, but have found some
useful info on it. Urls here in case they help anyone else:
http://www.sitepoint.com/article/php5-standard-library/2
http://www.ramikayyali.com/archives/2005/02/25/iterators
http://www.php.net/~helly/php/ext/spl/classArrayObject.html
http://www.devshed.com/index2.php?option=content&task=view&id=587&pop=1&hide_ads=1&page=0&hide_js=1
-------------
Finally, in my digging around on the net I found the following. I don't
100% follow the argument here, but it sounds like this guy is discussing
the same issue I came across (true?).
http://uk.php.net/manual/en/language.oop5.overloading.php
> A few things I've found about __get()...
>
> First off, if you use $obj-> getOne-> getAnother, both intended
> to be resolved by __get, the __get() function only sees the first
> one at first. You can't access the second one. You can, however,
> return the pointer to an object that can handle the second one.
> In short, you can have the same class handle both by returning a
> new object with the data changed however you see fit.
>
> Secondly, when using arrays like: $obj-> getArray["one"], only
> the array name is passed on to __get. However, when you return
> the array, PHP treats it just as it should. THat is, you'd have
> to make an array with the index of "one" in __get in order to see
> any results. You can also have other indexes in there as well.
>
> Also, for those of you like me, I've already tried to use
> func_get_args to see if you can get more than just that one.
>
> If you're like me and were hoping you could pass some sort of
> argument onto __get in order to help gather the correct data,
> you're out of look. I do recommend using __call though. You could
> easily rig __call up to react to certain things, like: $account->
> properties( "type" ); , which is my example. I'm using DOM for
> data storage (for now), and I'm trying to make an interface
> that'll let me easily switch to something else - MySQL, flat
> file, anything. This would work great though!
-------------
Thanks once again for all the help,
JON
Navigation:
[Reply to this message]
|