note 44591 added to ref.spl

From: Date: Tue, 10 Aug 2004 13:28:25 +0000
Subject: note 44591 added to ref.spl
Groups: php.notes 
Request: Send a blank email to php-notes+get-74400@lists.php.net to get a copy of this message
There are some interfaces used here that are not documented. It took a bit to figure this one out, but you can create your own ArrayObject type class (that is, one who's objects can be access using the array [$index] syntax). Your class must just implement ArrayAccess. Which has four abstract methods you must define. For example: <?php class Collection Implements ArrayAccess{ protected $array; function offsetExists($offset){ if(isset($this->array[$offset])){ return TRUE; } else{ return FALSE; } } function offsetGet($offset){ return $this->array[$offset]; } function offsetSet($offset, $value){ if($offset){ $this->array[$offset] = $value; } else{ $this->array[] = $value; } } function offsetUnset($offset){ } } ?> You'll have to jump through a couple more hoops to get foreach and print_r and the likes to behave properly. But with just this, you can : <?php $col = new Collction(); $col[] = new ObjectX(); $col[] = new ObjectX(123); echo $col[0]->name; // etc. ?> ---- Manual Page -- http://www.php.net/manual/en/ref.spl.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44591 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44591&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44591&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74400) next »