<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This class is a hand written simplified version of PHP native `ArrayObject`
* class, to show that it behaves differently than the PHP native implementation.
*/
class CustomArrayObject implements ArrayAccess, IteratorAggregate, Countable, Serializable
{
private $array;
public function __construct(array $array = null)
{
$this->array = $array ?: array();
}
public function offsetExists($offset)
{
return array_key_exists($offset, $this->array);
}
public function offsetGet($offset)
{
return $this->array[$offset];
}
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}
public function offsetUnset($offset)
{
unset($this->array[$offset]);
}
public function getIterator()
{
return new ArrayIterator($this->array);
}
public function count()
{
return count($this->array);
}
public function serialize()
{
return serialize($this->array);
}
public function unserialize($serialized)
{
$this->array = (array) unserialize((string) $serialized);
}
}
$array = array('foo' => 'bar', 'nulled' => null);
$native = new ArrayObject($array);
$custom = new CustomArrayObject($array);
foreach (array('array' => $array, 'native' => $native, 'custom' => $custom) as $type => $collection) {
echo 'Using the ' . $type .' implementation'.PHP_EOL;
var_dump(isset($collection['missing']) || array_key_exists('missing', $collection));
}
var_dump(isset($custom['missing']) || array_key_exists('missing', $custom));
Deprecated: Return type of CustomArrayObject::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 26
Deprecated: Return type of CustomArrayObject::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 31
Deprecated: Return type of CustomArrayObject::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 36
Deprecated: Return type of CustomArrayObject::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 45
Deprecated: Return type of CustomArrayObject::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 50
Deprecated: Return type of CustomArrayObject::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/m165G on line 55
Deprecated: CustomArrayObject implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /in/m165G on line 17
Using the array implementation
bool(false)
Using the native implementation
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, ArrayObject given in /in/m165G:77
Stack trace:
#0 {main}
thrown in /in/m165G on line 77
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Using the array implementation
bool(false)
Using the native implementation
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, ArrayObject given in /in/m165G:77
Stack trace:
#0 {main}
thrown in /in/m165G on line 77
Process exited with code 255.
Output for 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Using the array implementation
bool(false)
Using the native implementation
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/m165G on line 77
bool(false)
Using the custom implementation
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/m165G on line 77
bool(false)
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/m165G on line 79
bool(false)