PHP obsever pattern -
i using simple php observer pattern in zend. controller notifing particular observer. in input parameter id
, return type array
. still giving object return response in controller. subject follow:
<?php class newsubject implements subject{ public $observers = array(); function __construct() { } public function getdata(id){ $this->id = id; $this->notify(id); } public function attach(observer $obsever) { $id = spl_object_hash($obsever); $this->observers[$id] = $obsever; } public function detach(observer $obsever) { $id = spl_object_hash($obsever); unset($this->observers[$id]); } public function notify(id) { foreach($this->observers $obs) { $obs->update(id); } } } ?>
observer follow:
<?php class newobserver implements observer{ public function update($id) { $modelmethod = new modelmethod(); $modelmethod = $modelmethod->getdata($id); //which array return $modelmethod; } } ?>
call controller:
$subject = new newsubject(); $subject->attach(new newobserver()); $id = 2; $returnarray->getdata($id); print_r($returnarray);
output:
newsubject object ( [observers] => array ( [00000000243330360000000069e80d68] => newobserver)
which should return array.any suggestion. thanks.
lets stack trace flow -
$returnarray->getdata($id);
the above call getdata($id)
calls $this->notify(id);
.
notify()
has observers[]
array each key of array has objects.
foreach($this->observers $obs) /*$this->observers array*/ { $obs->update(id); /*but $obs seems object - individually*/ }
eg: observers = array ( object (...), object (...), object (...), );
have it.
Comments
Post a Comment