php - How to implement observation if we already used intheritance? -
there forumthread
class:
class forumthread extends dbtable { public function insert ($threadid, $comment) { sql insert parent::tablename values $threadid, $comment // email sending how? // putting on "notice-wall", how? } }
some other functions should done here, e.g. email sending. cant put here otherwise violate srp. cant put neither controller
, want insert post elsewhere too. im planning implement observed pattern, cant extend 2 classes.
with observer pattern have dispatch notification method in order execute relevant observers code.
you execute within insert method:
$this->notify('table_insertion', $data);
then somewhere else before notify line executed, event must registered so:
static::$observers['table_insertion'][] = array('class_to_call' => 'method_in_class_to_call');
the notify method like:
public function notify($event, $data) { foreach(static::$observer[$event] $class => $method) { new $class->$method($data); } }
hope makes sense.
Comments
Post a Comment