本文整理匯總了PHP中Subject::notify方法的典型用法代碼示例。如果您正苦於以下問題:PHP Subject::notify方法的具體用法?PHP Subject::notify怎麽用?PHP Subject::notify使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Subject
的用法示例。
在下文中一共展示了Subject::notify方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __construct
public function __construct()
{
$subject = new Subject();
$o1 = new Observer1();
$o2 = new Observer2();
$o3 = new Observer3();
$subject->setData('Data for each 3 attached observers!');
$subject->attach($o1);
$subject->attach($o2);
$subject->attach($o3);
$subject->notify();
$subject->detach($o3);
$subject->setData('Data not for 3rd detached one!');
$subject->notify();
}
示例2: attach
{
protected $observers = array();
public function attach(SplObserver $observer)
{
$this->observers[] = $observer;
}
public function detach(SplObserver $observer)
{
$key = array_search($observer, $this->observers);
if ($key) {
unset($this->observers[$key]);
}
}
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
}
class MyObserver implements SplObserver
{
public function update(SplSubject $subject)
{
echo "I was updated by " . get_class($subject);
}
}
$subject = new Subject();
$subject->attach(new MyObserver());
$subject->notify();
// prints "I was updated by Subject"