Page 1 of 1

[SOLVED] overloading classes with array accessor

Posted: Tue Aug 04, 2009 8:43 pm
by daaaaang
Hi!

I've been using Doctrine, and one of the features that I like (simply because it is pretty) is the ability to access member variables as if the object were an array. For example, all of the following lines are equivalent to each other:

Code: Select all

echo $user->username;
echo $user->get('username');
echo $user->getUsername;
echo $user['username'];
I know how to use __call to make the first three lines happen, but I've no idea how to emulate the fourth. I poured through a lot of the Doctrine code, but there is way too much of it for me to figure out this particular aspect.

Does anybody know how to do it, or even have any ideas? Let me know if I've been at all unclear.

Thank you!
Matt

Re: Syntactical sugar -- overloading classes with array accessor

Posted: Wed Aug 05, 2009 3:56 am
by Weirdan
daaaaang wrote:Does anybody know how to do it, or even have any ideas?
That's done by implementing ArrayAccess interface in your class. Docs are here: http://us2.php.net/arrayaccess

Re: Syntactical sugar -- overloading classes with array accessor

Posted: Wed Aug 05, 2009 1:21 pm
by daaaaang
Thank you so much!
Matt