<?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);
$a = array_key_exists('missing', $custom);
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/pBVtn 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/pBVtn 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/pBVtn 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/pBVtn 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/pBVtn 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/pBVtn 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/pBVtn on line 17
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, CustomArrayObject given in /in/pBVtn:75
Stack trace:
#0 {main}
thrown in /in/pBVtn on line 75
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, CustomArrayObject given in /in/pBVtn:75
Stack trace:
#0 {main}
thrown in /in/pBVtn on line 75
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/pBVtn on line 75
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /in/pBVtn on line 76
bool(false)