0

I'm trying to access array keys via object notation. I thought code below should work?

<?php

  $accountData = array('role'=> 'user');
  $accountDataAO = new ArrayObject($accountData, ArrayObject::STD_PROP_LIST);
  echo $accountDataAO->role;
?>

But I get this:

PHP Notice:  Undefined property: ArrayObject::$role in /home/stan/Desktop/test.php on line 5
PHP Stack trace:
PHP   1. {main}() /home/stan/Desktop/test.php:0

What am I doing wrong?

5
  • From php.net This class allows objects to work as arrays. ==> not arrays as objects. Eitherway, check the manual php.net/manual/en/class.arrayobject.php Commented Jan 24, 2014 at 13:18
  • ohh nvm. I figured it out. Had to add: ArrayObject::ARRAY_AS_PROPS Commented Jan 24, 2014 at 13:20
  • @Dannyboy update your original question then in case anyone else searches for the same thing. Commented Jan 24, 2014 at 13:21
  • @Styphon i've added it as an answer. Commented Jan 24, 2014 at 13:23
  • For future reference, this is demonstrated on the ArrayObject::setFlags() manual page. Commented Jan 24, 2014 at 13:30

1 Answer 1

3

ahh. nvm. I had to add: ArrayObject::ARRAY_AS_PROPS

<?php
  $accountData = array('role'=> 'user');
  $accountDataAO = new ArrayObject($accountData, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); 
  echo $accountDataAO->role;
?>
Sign up to request clarification or add additional context in comments.

Comments

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.