I have an object of class Foo:
class Foo extends Bar {
protected $a;
protected $b;
}
$obj = new Foo();
What I want (and have) to do is cast this object to an array, like this:
$arr = (array)$obj;
Is there any magic (or not magic :)) method that is being called at this moment? Or is there any other way to intercept it? I know I can write a simple method, eg. asArray()
in Foo, but I'm looking for some more "native" PHP ways.
You can have the class implement the ArrayAccess
interface. This will allow you to treat the object like an array without casting and you get total control over how the members are used.