Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nope, casting just results in a normal PHP array -- losing whatever functionality your ArrayObject-derived class had. Check this out:</p> <pre><code>class CaseInsensitiveArray extends ArrayObject { public function __construct($input = array(), $flags = 0, $iterator_class = 'ArrayIterator') { if (isset($input) &amp;&amp; is_array($input)) { $tmpargs = func_get_args(); $tmpargs[0] = array_change_key_case($tmpargs[0], CASE_LOWER); return call_user_func_array(array('parent', __FUNCTION__), $tmp args); } return call_user_func_array(array('parent', __FUNCTION__), func_get_args()); } public function offsetExists($index) { if (is_string($index)) return parent::offsetExists(strtolower($index)); return parent::offsetExists($index); } public function offsetGet($index) { if (is_string($index)) return parent::offsetGet(strtolower($index)); return parent::offsetGet($index); } public function offsetSet($index, $value) { if (is_string($index)) return parent::offsetSet(strtolower($index, $value)); return parent::offsetSet($index, $value); } public function offsetUnset($index) { if (is_string($index)) return parent::offsetUnset(strtolower($index)); return parent::offsetUnset($index); } } $blah = new CaseInsensitiveArray(array( 'A'=&gt;'hello', 'bcD'=&gt;'goodbye', 'efg'=&gt;'Aloha', )); echo "is array: ".is_array($blah)."\n"; print_r($blah); print_r(array_keys($blah)); echo $blah['a']."\n"; echo $blah['BCD']."\n"; echo $blah['eFg']."\n"; echo $blah['A']."\n"; </code></pre> <p>As expected, the array_keys() call fails. In addition, is_array($blah) returns false. But if you change the constructor line to:</p> <pre><code>$blah = (array)new CaseInsensitiveArray(array( </code></pre> <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> <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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload