Reputation: 33
I'm trying to understand this object but i can't figure out a simple fact. If count method shows public properties and the result is the number of keys in an array that was passed. In the case of an associative array when i try to access a key like a public property is not found. Maybe i misunderstood the interface.
//example
$currentDate = getdate();
//applying print_r() we can see the content
$objectDate = new ArrayObject();
//verifying the public properties- result is 11
$objectDate->count();
//but can't access keys like public properties
$objectDate->hours;
Upvotes: 0
Views: 71
Reputation: 59927
You can access array entries as properties (->
) by passing the ArrayObject::ARRAY_AS_PROPS
flag to the ArrayObject
constructor:
//example
$currentDate = getdate();
print_r($currentDate);
// create ArrayObject from array, make entries accessible as properties (read and write).
$objectDate = new ArrayObject($currentDate, ArrayObject::ARRAY_AS_PROPS);
// verifying the public methods - result is 11
print_r($objectDate->count());
print "\n";
// accessing entries like public properties
print_r($objectDate->hours);
Upvotes: 3
Reputation: 22817
Such class implements ArrayAccess
interface, so you can write:
$objectDate['hours']
With brackets notation, but not with arrow [->
] one.
Upvotes: 2