当前位置: 首页>>代码示例>>PHP>>正文


PHP Subject::attach方法代码示例

本文整理汇总了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());
 }
开发者ID:metabor,项目名称:statemachine,代码行数:14,代码来源:SubjectTest.php

示例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();
 }
开发者ID:rodolfobandeira,项目名称:observer,代码行数:15,代码来源:Client.php

示例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();
 }
开发者ID:YidaChen,项目名称:tutorial-php-advenced,代码行数:15,代码来源:MockTest.php

示例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);
开发者ID:manachyn,项目名称:trainings,代码行数:31,代码来源:Observer1.php

示例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');
开发者ID:robotomize,项目名称:php-oop-patterns,代码行数:15,代码来源:example.php

示例6: test

 public static function test()
 {
     $subject = new Subject();
     $subject->attach(new Observer1());
     $subject->attach(new Observer2());
     $subject->post();
     //输出:数据量加1 积分加10
 }
开发者ID:YexuanGuo,项目名称:php-note,代码行数:8,代码来源:summed.php

示例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('这是内容');
开发者ID:breezelife0,项目名称:mycode,代码行数:31,代码来源:5.php

示例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);
开发者ID:ralf000,项目名称:PHP4,代码行数:31,代码来源:104-observer.php

示例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"
开发者ID:karimo255,项目名称:blog,代码行数:31,代码来源:test.php

示例10: __construct

 /**
  * @param Subject $subject
  */
 public function __construct(Subject $subject = null)
 {
     if (!empty($subject)) {
         $subject->attach($this);
     }
 }
开发者ID:rpodwika,项目名称:designpatterns,代码行数:9,代码来源:Observer.php

示例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);
 }
开发者ID:Burgestrand,项目名称:phplibs,代码行数:8,代码来源:SplSubjectAdapter.php


注:本文中的Subject::attach方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。