<?php
class ArrayStorage implements Iterator, ArrayAccess
{
private $holder = [];
private $instanceName;
public function __construct($instanceName)
{
if (!class_exists($instanceName)) {
throw new \Exception('Class '.$instanceName.' was not found');
}
$this->instanceName = $instanceName;
}
public function rewind()
{
reset($this->holder);
}
public function current()
{
return current($this->holder);
}
public function key()
{
return key($this->holder);
}
public function next()
{
next($this->holder);
}
public function valid()
{
return false !== $this->current();
}
public function offsetSet($offset, $value)
{
if (!($value instanceof $this->instanceName)) {
throw new \Exception('Storage allows only '.$this->instanceName.' instances');
}
if (is_null($offset)) {
$this->holder[] = $value;
} else {
$this->holder[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->holder[$offset]);
}
public function offsetUnset($offset)
{
unset($this->holder[$offset]);
}
public function offsetGet($offset)
{
return isset($this->holder[$offset]) ? $this->holder[$offset] : null;
}
}
class Foo {}
class Bar {}
$storage = new ArrayStorage('Foo');
$storage[] = new Foo;
$storage['baz'] = new Foo;
//var_export($storage);
foreach ($storage as $key => $value) {
echo($key. ' => '.PHP_EOL.var_export($value, 1).PHP_EOL);
}
$storage['bee'] = new Bar;
Deprecated: Return type of ArrayStorage::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 21
Deprecated: Return type of ArrayStorage::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 31
Deprecated: Return type of ArrayStorage::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 26
Deprecated: Return type of ArrayStorage::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 36
Deprecated: Return type of ArrayStorage::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 16
Deprecated: Return type of ArrayStorage::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/4jWtD on line 53
Deprecated: Return type of ArrayStorage::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/4jWtD on line 63
Deprecated: Return type of ArrayStorage::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/4jWtD on line 41
Deprecated: Return type of ArrayStorage::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/4jWtD on line 58
0 =>
\Foo::__set_state(array(
))
baz =>
\Foo::__set_state(array(
))
Fatal error: Uncaught Exception: Storage allows only Foo instances in /in/4jWtD:44
Stack trace:
#0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar))
#1 {main}
thrown in /in/4jWtD on line 44
Process exited with code 255.
Output for 8.1.0 - 8.1.28
Deprecated: Return type of ArrayStorage::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 21
Deprecated: Return type of ArrayStorage::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 31
Deprecated: Return type of ArrayStorage::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 26
Deprecated: Return type of ArrayStorage::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 36
Deprecated: Return type of ArrayStorage::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 16
Deprecated: Return type of ArrayStorage::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/4jWtD on line 53
Deprecated: Return type of ArrayStorage::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/4jWtD on line 63
Deprecated: Return type of ArrayStorage::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/4jWtD on line 41
Deprecated: Return type of ArrayStorage::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/4jWtD on line 58
0 =>
Foo::__set_state(array(
))
baz =>
Foo::__set_state(array(
))
Fatal error: Uncaught Exception: Storage allows only Foo instances in /in/4jWtD:44
Stack trace:
#0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar))
#1 {main}
thrown in /in/4jWtD on line 44
Process exited with code 255.