Immutable custom PHP array containing objects of a certain type -


firstly want create sort of array allows addition of type of object; myobject.

secondly want array immutable, value object array. want add myobjects on creation, access them using foreach or using offsets $myarray[0] don't want items added, removed or sorted - or items changed.

so have looked arrayobject, arrayiterator , arrayaccess. can tell, these allow object act array. i'm pretty sure that's not want since php array functionality not class can extend, im not sure path take.

my current implementation dumbed down class. cannot used in foreach or using array offsets $myarray[0] without calling getarray function first.

foreach ($myarray->getarray() $myobjecy) { 

it's best can think of there must better way of doing this?

mycustomarray {     protected $array = [];      public __construct(array $array)     {         foreach($array $obj) {             if (! $obj instanceof \myobject) {                 throw new \exception();             }         }          $this->array = $array;     }      public getarray()     {         return $this->array;     }      // stop people dynamically adding properties     public function __set($name, $value) {         throw new \exception();     } } 


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -