0

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;
3
  • 1
    Minus, because this is "I'm too lazy to read docs" question. Commented Apr 17, 2014 at 8:54
  • Beside the fact that you've to use ['hours'], you need a $ in front of objectDate->hours;. Commented Apr 17, 2014 at 8:57
  • @RafałWalczak, did you read the docs? It's very unclear if you ask me. Upvoting because it's an interesting question and because Rafal is incorrect ;) Commented Apr 17, 2014 at 9:03

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

Such class implements ArrayAccess interface, so you can write:

$objectDate['hours']

With brackets notation, but not with arrow [->] one.

1 Comment

Ok! Thanks! I see my problem now

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.