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
}
You're mixing objects and arrays. You can create and access an object like so:
and an array like so:
You can define a class and add implements ArrayAccess if you want to access your class as both an array and a class.
http://www.php.net/manual/en/language.oop5.php
You could also cast the object as an array:
Try extending ArrayObject
You'll also need to implement a
__get
Magic Method as Valentin Golev mentioned.Your class will need to looks something like this:
Just add
implements ArrayAccess
to your class and add the required methods:See http://php.net/manual/en/class.arrayaccess.php
ArrayObject implements the ArrayAccess interface (and some more). Using the ARRAY_AS_PROPS flag it provides the functionality you're looking for.
Alternatively you can implement the ArrayAccess interface in one of your own classes:
Enhance Class capability with no functionality drawbacks
You can also use ArrayAccess to access a single array property in your class and leave other properties being accessed in OOP way. Yet still it will work as you requested.
The above will work as you expected.
Also note that Foo['name'] !== Foo->getName() those a two different variables