本文整理汇总了PHP中Subject::attach方法的典型用法代码示例。如果您正苦于以下问题:PHP Subject::attach方法的具体用法?PHP Subject::attach怎么用?PHP Subject::attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subject
的用法示例。
在下文中一共展示了Subject::attach方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testObserverCanBeAttachedAndDetach
/**
*
*/
public function testObserverCanBeAttachedAndDetach()
{
$subject = new Subject();
$observerA = $this->getMockForAbstractClass('\\SplObserver');
$subject->attach($observerA);
$observerB = $this->getMockForAbstractClass('\\SplObserver');
$subject->attach($observerB);
$subject->detach($observerB);
$this->assertContains($observerA, $subject->getObservers());
$this->assertNotContains($observerB, $subject->getObservers());
}
示例2: __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();
}
示例3: testUpdateIsCalledOnce
/**
* @group fakeobject
*/
public function testUpdateIsCalledOnce()
{
// 建立一個 Observer 的 Mock Object
$observer = $this->getMock('Observer');
// 預期 Observer::update 方法應該只跑一次
// 而傳入 update 方法的參數值為 something
$observer->expects($this->once())->method('update')->with($this->equalTo('something'));
$subject = new Subject();
$subject->attach($observer);
// 我們預測這裡會呼叫 Observer::update() 一次
$subject->doSomething();
}
示例4: notify
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function increaseValue($increaseBy)
{
$this->value += $increaseBy;
$this->notify();
}
public function getValue()
{
return $this->value;
}
}
class ValueObserver implements SplObserver
{
public function update(SplSubject $subject)
{
if ($subject->getValue() > 20) {
echo "Value is greater than 20!";
}
}
}
$subject = new Subject();
$subject->attach(new ValueObserver());
$subject->increaseValue(10);
$subject->increaseValue(10);
$subject->increaseValue(10);
$subject->increaseValue(10);
示例5: Subject
<?php
namespace Observer;
require_once 'AbstractObserver.php';
require_once 'AbstractSubject.php';
require_once 'Observer.php';
require_once 'Subject.php';
$patternGossiper = new Subject();
$patternGossipFan = new Observer();
$patternGossiper->attach($patternGossipFan);
$patternGossiper->updateFavorites('abstract factory, decorator, visitor');
$patternGossiper->updateFavorites('abstract factory, observer, decorator');
$patternGossiper->detach($patternGossipFan);
$patternGossiper->updateFavorites('abstract factory, observer, paisley');
示例6: test
public static function test()
{
$subject = new Subject();
$subject->attach(new Observer1());
$subject->attach(new Observer2());
$subject->post();
//输出:数据量加1 积分加10
}
示例7: update
{
return $this->_value;
}
}
/**
* 观察者1
*/
class Observer1 implements SplObserver
{
public function update(SplSubject $subject)
{
echo '<b>对象</b>:</b>' . get_class() . ' <b>内容:</b>' . $subject->getValue() . '<br />';
}
}
/**
* 观察者2
*/
class Observer2 implements SplObserver
{
public function update(SplSubject $subject)
{
echo '<b>对象:</b>' . get_class() . ' <b>内容:</b>' . $subject->getValue() . '<br />';
}
}
//实现
$subject = new Subject();
$object1 = new Observer1();
$object2 = new Observer2();
$subject->attach($object1);
$subject->attach($object2);
$subject->setValue('这是内容');
示例8: notify
if ($id = array_search($observer, $this->observers, true)) {
unset($this->observers[$id]);
}
}
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function setValue($value)
{
$this->value = $value;
$this->notify();
}
public function getValue()
{
return $this->value;
}
}
class Observer implements SplObserver
{
public function update(SplSubject $subject)
{
echo '<p>Новое значение равно: ' . $subject->getValue() . '</p>';
}
}
$subject = new Subject();
$observer = new Observer();
$subject->attach($observer);
$subject->setValue(5);
示例9: 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"
示例10: __construct
/**
* @param Subject $subject
*/
public function __construct(Subject $subject = null)
{
if (!empty($subject)) {
$subject->attach($this);
}
}
示例11: attach
/**
* Same as {@link Subject::attach}, but it takes only one argument and
* that argument must be a class which implements the SplObserver interface.
*/
public function attach(SplObserver $obj)
{
return parent::attach($obj);
}