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
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 :".
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.
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.
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:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With