3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php # http://scribu.net/blog/defaultdict-in-php.html class Defaultdict implements ArrayAccess { private $container = array(); private $default; public function __construct( $default ) { $this->default = $default; } public function offsetSet( $offset, $value ) { if ( is_null($offset) ) { trigger_error( sprintf( "Trying to use %s as a list.", __CLASS__ ), E_USER_WARNING ); return; } $this->container[$offset] = $value; } public function offsetExists( $offset ) { return true; } public function offsetUnset( $offset ) { unset( $this->container[$offset] ); } public function &offsetGet( $offset ) { if ( !isset( $this->container[$offset] ) ) { if ( is_callable($this->default) ) $value = call_user_func( $this->default, $offset ); else $value = $this->default; $this->container[$offset] = $value; } return $this->container[$offset]; } } $counts = new Defaultdict(1); $counts['English']++; print_r(array_keys($counts));
based on okK94
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.1
Warning: array_keys() expects parameter 1 to be array, object given in /in/nkm4c on line 35
Output for hhvm-3.15.4
Warning: array_keys() expects parameter 1 to be an array or collection in /in/nkm4c on line 35