- var_dump: documentation ( source)
- reset: documentation ( source)
- print_r: documentation ( source)
- key: documentation ( source)
- next: documentation ( source)
- current: documentation ( source)
<?php
class MyModel
{
}
class Collection implements \ArrayAccess, \Iterator
{
private $storage = array();
public function offsetExists($offset)
{
return isset($this->storage[$offset]);
}
public function offsetGet($offset)
{
return $this->storage[$offset];
}
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->storage[] = $value;
} else {
$this->storage[$offset] = $value;
}
}
public function offsetUnset($offset)
{
echo 'trying to unset ' . $offset . PHP_EOL;
unset($this->storage[$offset]);
var_dump($this->storage);
}
public function current()
{
return current($this->storage);
}
public function next()
{
return next($this->storage);
}
public function key()
{
return key($this->storage);
}
public function valid()
{
return key($this->storage) !== null;
}
public function rewind()
{
reset($this->storage);
}
public function count()
{
return count($this->storage);
}
}
class MyCollectionModel
{
private $collection;
public function __construct()
{
$this->collection = new Collection();
}
public function addElement($element)
{
$this->collection[] = $element;
}
public function removeMyModels()
{
foreach ($this->collection as $index => $value) {
var_dump($index);
if ($value instanceof MyModel) {
unset ($this->collection[$index]);
}
}
}
}
// Создем коллекцию
$myCollection = new MyCollectionModel();
// Заполняем своими можельками
for ($i = 0; $i < 10; $i++) {
$myCollection->addElement(new MyModel());
}
// Удаляем свои модельки
$myCollection->removeMyModels();
// Сука, ебучая PHP!!!
print_r($myCollection);