views:

80

answers:

2

I have seen something like this in an ORM:

    $b = new Book();
    $b->limit(5)->get();

    echo 'ID: ' . $b->id . '<br />';
    echo 'Name: ' . $b->title . '<br />';
    echo 'Description: ' . $b->description . '<br />';
    echo 'Year: ' . $b->year . '<br />';

    foreach ($b as $book)
    {
        echo 'ID: ' . $book->id . '<br />';
        echo 'Name: ' . $book->title . '<br />';
        echo 'Description: ' . $book->description . '<br />';
        echo 'Year: ' . $book->year . '<br />';
        echo '<br />';
    }

How is it possible that an object acts as both array and object? How can I accomplish that? I was hoping to see a new __magic method or something in Book's parent class, but I couldn't find anything, so there might be something really basic about php objects that I don't know.

Any thoughts? Thanks in advance

+5  A: 

Objects that implement the Traversable interface (through Iterator or IteratorAggregate) support the foreach construct, if that's what you mean by "acting as an array." Eg:

class theClass implements Iterator {
    // implement iterator here
}

// now you can do
$obj = new theClass;

foreach ($obj as $value) {
    // do something
}
NullUserException
Gotta love this! :)
balupton
yes that's what I ment, thanks!
Anonymous Noob
Actually every object can be taversed with foreach, but it will access all public properties. You need to implement `Iterator` if you want to change this behaviour. Another interface you might be interested in is `ArrayAccess`
Mchl
+2  A: 

You do not have to do anything special to use foreach with objects.

From the PHP manual on Object Iteration:

PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.

Example:

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

foreach(new Foo as $prop) echo $prop; // outputs 1 only

Your class does not have to implement Traversable as suggested elsewhere and in fact, the class above doesn't:

var_dump (new Foo instanceof Traversable); // FALSE

You can implement one of the Iterator or IteratorAggregate if you need more control over how the iteration should behave:

class Foo implements IteratorAggregate
{
    public $foo = 1;
    protected $bar = 2;
    private $baz = 3;

    public function getIterator()
    {
        return new ArrayIterator((array) $this);
    }
}

foreach(new Foo as $prop) echo $prop; // outputs 123

Because Iterator and IteratorAggregate extend Traversable, your class will now also be an instance of Traversable, but like shown above, it's not necessary for iterating the object.

var_dump (new Foo instanceof Traversable); // TRUE

You can also use an ArrayObject to make the object behave like a hybrid between class and object. Or you can implement ArrayAccess to allow accessing the class with square brackets. You can also subclass the class to be one of the Iterators PHP provides.

Further reading:

Gordon