Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object acting like an array? (PHP)

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

like image 972
Anonymous Noob Avatar asked Sep 21 '10 05:09

Anonymous Noob


People also ask

How do you object an array in PHP?

Converting an object to an array with typecasting technique: php class bag { public function __construct( $item1, $item2, $item3){ $this->item1 = $item1; $this->item2 =$item2; $this->item3 = $item3; } } $myBag = new bag("Books", "Ball", "Pens"); echo "Before conversion :".

Is PHP an object or array?

Definition and Usage. The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.

What does => mean in PHP?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.


2 Answers

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:

  • http://www.phpro.org/tutorials/Introduction-to-SPL.html
  • http://www.phpro.org/tutorials/Introduction-to-SPL-ArrayAccess.html
  • PHP object like array
like image 152
Gordon Avatar answered Oct 25 '22 20:10

Gordon


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
}
like image 41
NullUserException Avatar answered Oct 25 '22 20:10

NullUserException