本文整理汇总了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"