<p>I'd like to be able to write a PHP class that behaves like an array and uses normal array syntax for getting &amp; setting.</p>

<p>For example (where Foo is a PHP class of my making):</p>

<pre><code>$foo = new Foo();

$foo['fooKey'] = 'foo value';

echo $foo['fooKey'];
</code></pre>

<p>I know that PHP has the _get and _set magic methods but those don't let you use array notation to access items.  Python handles it by overloading __getitem__ and __setitem__.</p>

<p>Is there a way to do this in PHP?  If it makes a difference, I'm running PHP 5.2.</p>

[{'answer_id': 65136, 'author': 'Mat Mannion', 'author_id': 6282, 'author_profile': 'https://Stackoverflow.com/users/6282', 'pm_score': 6, 'selected': True, 'text': '<p>If you extend <code>ArrayObject</code> or implement <code>ArrayAccess</code> then you can do what you want.</p>\n\n<ul>\n<li><a href="http://php.net/arrayobject" rel="noreferrer">ArrayObject</a></li>\n<li><a href="http://php.net/arrayaccess" rel="noreferrer">ArrayAccess</a></li>\n</ul>\n'}, {'answer_id': 5986293, 'author': 'Ron Cemer', 'author_id': 751626, 'author_profile': 'https://Stackoverflow.com/users/751626', 'pm_score': 2, 'selected': False, 'text': '<p>Nope, casting just results in a normal PHP array -- losing whatever functionality your ArrayObject-derived class had.  Check this out:</p>\n\n<pre><code>class CaseInsensitiveArray extends ArrayObject {\n    public function __construct($input = array(), $flags = 0, $iterator_class =     \'ArrayIterator\') {\n        if (isset($input) &amp;&amp; is_array($input)) {\n            $tmpargs = func_get_args();\n            $tmpargs[0] = array_change_key_case($tmpargs[0], CASE_LOWER);\n            return call_user_func_array(array(\'parent\', __FUNCTION__), $tmp    args);\n        }\n        return call_user_func_array(array(\'parent\', __FUNCTION__), func_get_args());\n    }\n\n    public function offsetExists($index) {\n        if (is_string($index)) return parent::offsetExists(strtolower($index));\n        return parent::offsetExists($index);\n    }\n\n    public function offsetGet($index) {\n        if (is_string($index)) return parent::offsetGet(strtolower($index));\n        return parent::offsetGet($index);\n    }\n\n    public function offsetSet($index, $value) {\n        if (is_string($index)) return parent::offsetSet(strtolower($index, $value));\n        return parent::offsetSet($index, $value);\n    }\n\n    public function offsetUnset($index) {\n        if (is_string($index)) return parent::offsetUnset(strtolower($index));\n        return parent::offsetUnset($index);\n    }\n}\n\n$blah = new CaseInsensitiveArray(array(\n    \'A\'=&gt;\'hello\',\n    \'bcD\'=&gt;\'goodbye\',\n    \'efg\'=&gt;\'Aloha\',\n));\n\necho "is array: ".is_array($blah)."\\n";\n\nprint_r($blah);\nprint_r(array_keys($blah));\n\necho $blah[\'a\']."\\n";\necho $blah[\'BCD\']."\\n";\necho $blah[\'eFg\']."\\n";\necho $blah[\'A\']."\\n";\n</code></pre>\n\n<p>As expected, the array_keys() call fails.  In addition, is_array($blah) returns false.  But if you change the constructor line to:</p>\n\n<pre><code>$blah = (array)new CaseInsensitiveArray(array(\n</code></pre>\n\n<p>then you just get a normal PHP array (is_array($blah) returns true, and array_keys($blah) works), but all of the functionality of the ArrayObject-derived subclass is lost (in this case, case-insensitive keys no longer work).  Try running the above code both ways, and you\'ll see what I mean.</p>\n\n<p>PHP should either provide a native array in which the keys are case-insensitive, or make ArrayObject be castable to array without losing whatever functionality the subclass implements, or just make all array functions accept ArrayObject instances.</p>\n'}]
['https://Stackoverflow.com/questions/65091', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']