phpmagic-function

Using __get() on array class properties to auto-propogate with a class


I have a activerecord model which has an array in it of say: "$sessions". What I would like to do is make this a 2d array so as to be indexed in the following way:

["0"=>Session(Object), "1"=>Session(Object)]

Now to add cream on top I would like to make it so the user could do:

user->sessions[1]->id = "ghgh"

And the __get would understand whether there is an object already in that position in the class variable ("$sessions") and add/edit the property to the class or it will make a new class in that position called "Session".

I am a bit confused how I could get PHP (if possible) to get a __get on an index of an array.


Solution

  • I think you're looking for ArrayAccess.

    Interface to provide accessing objects as arrays.