I need to be able to set my object like this:
$obj->foo = 'bar';
then after that is set i need the following to be true
if($obj['foo'] == 'bar'){
//more code here
}
I need to be able to set my object like this:
$obj->foo = 'bar';
then after that is set i need the following to be true
if($obj['foo'] == 'bar'){
//more code here
}
Your object must implement the
ArrayAccess
interface, then PHP will allow you to use the square brackets like that.You can access PHP object as PHP array, but in different ways. Try this:
That is similar with accessing array like this:
You can also do this:
You'll have to implement the
ArrayAccess
interface to be able to do that -- which only means implementing a few (4 to be exact) simple methods :ArrayAccess::offsetExists
: Whether or not an offset exists.ArrayAccess::offsetGet
: Returns the value at specified offset.ArrayAccess::offsetSet
: Assigns a value to the specified offset.ArrayAccess::offsetUnset
: Unsets an offset.There is a full example on the manual's page I pointed to ;-)