note 92185 added to language.oop5.patterns

From: Date: Mon, 13 Jul 2009 15:48:10 +0000
Subject: note 92185 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-158232@lists.php.net to get a copy of this message
Example of a decorator, which adds array access to any object: <?php class ArrayAccessDecorator implements ArrayAccess { protected $object = null; public function __construct($object){ $this->object = $object; } public function __call($method, $args){ return $this->productpart->$method($args); } public function __set($key, $val){ $this->object->$key = $val; } public function __get($key){ return $this->object->$key; } /* ArrayAccess */ public function offsetSet($key, $value){ $this->__set($key, $value); } public function offsetGet($key){ return $this->__get($key); } public function offsetUnset($key){ $this->__set($key, null); } public function offsetExists($offset){ $test = $this->object->__get($offset); if($test !== null){ return true; } else { return false; } } } $stdobject = (object)array('test'=>'test', 'test2' => 'test2'); $arr = new ArrayAccessDecorator($stdobject); print $arr['test']; // test $arr['test'] = 'hello'; print $arr['test']; // hello ?> ---- Server IP: 91.199.167.18 Probable Submitter: 92.65.202.82 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/92185 Del: integrated -- https://master.php.net/note/delete/92185/integrated Del: useless -- https://master.php.net/note/delete/92185/useless Del: bad code -- https://master.php.net/note/delete/92185/bad+code Del: spam -- https://master.php.net/note/delete/92185/spam Del: non-english -- https://master.php.net/note/delete/92185/non-english Del: in docs -- https://master.php.net/note/delete/92185/in+docs Del: other reasons-- https://master.php.net/note/delete/92185 Reject -- https://master.php.net/note/reject/92185 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158232) next »