- substr: documentation ( source)
- array_merge: documentation ( source)
- is_callable: documentation ( source)
<?php
trait GenericList {
public function __call($func, $argv)
{
echo "__call called \n";
if (!is_callable($func) || substr($func, 0, 6) !== 'array_')
{
throw new \BadMethodCallException(__CLASS__.'->'.$func);
}
return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
}
final public function offsetUnset($offset) {
parent::offsetUnset($offset);
}
final public function sort($preserveKeys = false) {
if($preserveKeys)
parent::ksort();
else
parent::exchangeArray (\Arr::reindex(parent::getArrayCopy ()));
}
final public function clear(){
parent::exchangeArray([]);
}
}
final class LineItemList extends \ArrayObject {
use GenericList;
public function offsetSet($offset, $value) {
if (!($value instanceof LineItemBase)) {
throw new \InvalidArgumentException("Value has to be a instance of LineItemBase");
}
parent::offsetSet($offset, $value);
}
}
class LineItemBase{
public $blah;
public function __construct(string $blah){
$this->blah = $blah;
}
}
$list = new LineItemList();
$list []= new LineItemBase('SDSdSD');
echo $list [0]->blah;
$list->array_search('adf');