46

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.

6 Answers 6

52

No

There is no __toArray magic method in PHP. An enhancement proposal has been rejected in 2006 with the following answer:

[2006-08-20 11:12 UTC] [email protected]

Why not simply have a method asArray() maybe even as par of an interface:

interface ArrayConversion { function asArray(); }

See, we have __toString as it is supported in language constructs like echo, print and other internal functions. But we decided against an autoconversion for arrays already. So itwill never be supported in any language construct. That said there is no needed for this and nothing you would win against the above interface. In fact you would make it php more complex because you'd add just one more magic feature.

It is thus very unlikely that it will be implemented in any future release (which is a pity, if you ask me).

4
  • 14
    but hey, didn't we get the goto construct at least? shivers
    – tacone
    Dec 27, 2013 at 13:16
  • 1
    It's strange he should say so, cause actually php has an object to array convertion: php.net/manual/en/…
    – Dienow
    Jan 21, 2014 at 15:03
  • 1
    The question is actually about being able to implement custom casting logic.
    – tacone
    Jan 21, 2014 at 16:55
  • 4
    Just a note: In 2019, PHP is now discussing __toArray() : wiki.php.net/rfc/to-array Nov 29, 2020 at 23:51
46

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.

3
  • 46
    Though nice, this answer doesn't really address the question. You should at least include the word "no" in there somewhere :)
    – Hubro
    Apr 29, 2013 at 8:39
  • 2
    Good point, but state somewhere at the beginning of the answer that implementing the ArrayAccess interface is not sufficient to cast an object to an array. Dec 22, 2015 at 0:38
  • 2
    Should also be noted that in order to loop over elements contained within the object that extends ArrayAccess using foreach, you must also implement \IteratorAggregate or \Iterator. Generally those looking to reproduce array functionality within an object use implements \Countable, \IteratorAggregate, \ArrayAccess See \ArrayObject.
    – Will B.
    Jan 22, 2016 at 15:31
15

Sadly no, casting to array doesn't trigger any magic method like it is done with:

$s = (string)$obj;

which triggers __toString() method and which you can override.

However, you may write a custom toArray() method.

You may also be interested in the Serializable interface which allows you to write custom serializer strategy.

0
4

Not sure if this question still relevant, but php has builtin ArrayObject class, that allows to treat object as array and can be handy when used as container for database record or collection.

It might be not a best practice in regards to strict types, but it allows to treat object as array and both statements are valid.

$obj = new ArrayObject(['a' => 'alpha']);
var_dump($obj['a']); //alpha
var_dump($obj->getOffset('a'));//alpha

However you need to keep in mind behaviour of ArrayObject

$obj = new ArrayObject(['a' => 'alpha']);
//Access Property
var_dump($obj['a']); //alpha
var_dump($obj->offsetGet('a'));//alpha
var_dump($obj->a); //null Notice: Undefined property: ArrayObject::$a

//Serialization
var_dump(serialize($obj));// string 'C:11:"ArrayObject":41:{x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}}' (length=65)
var_dump($obj->serialize());// string 'x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}'
var_dump(serialize($obj) === $obj->serialize());// false !!!

//Setting Properties
$obj['b'] = 'beta'; //OK
$obj->c = 'gamma'; //value becomes object property!!!
var_dump($obj);
/* OBJECT DUMP
object(ArrayObject)[13]
  public 'c' => string 'gamma' (length=5)
  private 'storage' =>
    array (size=2)
      'a' => string 'alpha' (length=5)
      'b' => string 'beta' (length=4)
 */

//Property validation as array
var_dump(isset($obj['a']));//true
var_dump(isset($obj['b']));//true
var_dump(isset($obj['c']));//false
//Property validation as object
var_dump(isset($obj->a));//false
var_dump(isset($obj->b));//false
var_dump(isset($obj->c));//true

//Typecasting
var_dump((array)$obj);
/*
array (size=2)
  'a' => string 'alpha' (length=5)
  'b' => string 'beta' (length=4)
 */

//var_dump((string)$obj);// Catchable fatal error: Object of class ArrayObject could not be converted to string

ArrayObject accepts two flags ArrayObject::STD_PROP_LIST as default and ArrayObject::ARRAY_AS_PROPS as alternative.

This would change behaviour for reading values but does not support setting new properties in that way, here is example:

$obj = new ArrayObject(['a' => 'alpha'], ArrayObject::ARRAY_AS_PROPS);
//Access Property
var_dump($obj['a']); //alpha
var_dump($obj->offsetGet('a'));//alpha
var_dump($obj->a);//alpha

//Serialization
var_dump(serialize($obj));// string 'C:11:"ArrayObject":41:{x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}}' (length=65)
var_dump($obj->serialize());// string 'x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}'
var_dump(serialize($obj) === $obj->serialize());// false !!!

//Setting Properties
$obj['b'] = 'beta'; //OK
$obj->c = 'gamma'; //OK
var_dump($obj);
/* OBJECT DUMP
object(ArrayObject)[14]
  private 'storage' =>
    array (size=3)
      'a' => string 'alpha' (length=5)
      'b' => string 'beta' (length=4)
      'c' => string 'gamma' (length=5)
 */

//Property validation as array
var_dump(isset($obj['a']));//true
var_dump(isset($obj['b']));//true
var_dump(isset($obj['c']));//false !!!
//Property validation as object
var_dump(isset($obj->a));//true
var_dump(isset($obj->b));//true
var_dump(isset($obj->c));//true

//Typecasting
var_dump((array)$obj);
/*
array (size=2)
  'a' => string 'alpha' (length=5)
  'b' => string 'beta' (length=4)
 */

To make this behaviour more consistent you would have to extend this class and implement magic methods __get(), __set(), __isset() and __unset().

Another tricky part is serialization, default method serialize would return you a copy serialized $storage variable instead of object itself, as workaround to return serialized copy of instance you can implement default serialization in __toString method, this way it's behave correctly.

class FooObject extends ArrayObject
{
    public function __get($index)
    {
        if ($this->offsetExists($index)) {
            return $this->offsetGet($index);
        } else {
            throw new UnexpectedValueException('Undefined key ' . $index);
        }
    }

    public function __set($index, $value)
    {
        $this->offsetSet($index, $value);
        return $this;
    }

    public function __isset($index)
    {
        return $this->offsetExists($index);
    }

    public function __unset($index)
    {
        return $this->offsetUnset($index);
    }

    public function __toString()
    {
        return serialize($this);
    }
}

Example of usage

$obj2 = new FooObject(['a' => 'alpha']);
//Access Property
var_dump($obj2['a']); //alpha
var_dump($obj2->offsetGet('a'));//alpha
var_dump($obj2->a); //alpha

//Serialization
var_dump(serialize($obj));// string 'C:11:"ArrayObject":41:{x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}}' (length=65)
var_dump($obj->serialize());// string 'x:i:0;a:1:{s:1:"a";s:5:"alpha";};m:a:0:{}'
var_dump(serialize($obj) === $obj->serialize());// false !!!

//Setting Properties
$obj2['b'] = 'beta'; //OK
$obj2->c = 'gamma'; //OK
var_dump($obj2);
/* OBJECT DUMP
object(FooObject)[14]
  private 'storage' (ArrayObject) =>
    array (size=3)
      'a' => string 'alpha' (length=5)
      'b' => string 'beta' (length=4)
      'c' => string 'gamma' (length=5)
 */

//Property validation as array
var_dump(isset($obj2['a']));//true
var_dump(isset($obj2['b']));//true
var_dump(isset($obj2['c']));//true
//Property validation as object
var_dump(isset($obj2->a));//true
var_dump(isset($obj2->b));//true
var_dump(isset($obj2->c));//true

//Typecasting
var_dump((array)$obj2);
/*
array (size=3)
  'a' => string 'alpha' (length=5)
  'b' => string 'beta' (length=4)
  'c' => string 'gamma' (length=5)
 */
2

One way to do this, without changing the original class definition is to use reflection. This allows to you examine the class's properties at runtime.

Taken from the manual: http://www.php.net/manual/en/reflectionclass.getproperties.php

<?php
class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
    print $prop->getName() . "\n";
}

var_dump($props);

?>

The above example will output something similar to:
foo
bar
array(2) {
  [0]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "foo"
    ["class"]=>
    string(3) "Foo"
  }
  [1]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "Foo"
  }
}
1

You can use get_object_vars($yourObject) that will return an associative array of all property names/values accessible from the context.

See http://php.net/manual/en/function.get-object-vars.php

A you want to access protected or private properties, my advice would be to extend ArrayObject, that implements the method getArrayCopy()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.