當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。