Home  >  Article  >  Backend Development  >  Implementation of PHP5+ standard function library observer_PHP tutorial

Implementation of PHP5+ standard function library observer_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:25926browse

Implementation of PHP5+ standard function library observer

The implementation of PHP's observer design pattern is relatively simple, but the PHP5+ version already has standard library support, and we only need to simply inherit and implement it.

Observer: Implements the standard interface class library SplSubject. One registration method: attach, one deregistration method: detach. A notification method: nofity.

<?php

class TSPLSubject implements SplSubject{

	 private $observers, $value;

	 public function __construct(){
		$this->observers =array();
	 }

	 public function attach(SplObserver $observer){
		$this->observers[] = $observer;
	 }

	 public function detach(SplObserver $observer){
		if($idx = array_search($observer, $this->observers,true)) {
			unset($this->observers[$idx]);
		}
	 }
	 
    /**
	 *
	 * Notify observers one by one (main entry)
	 *
	 * @param none
	 * @return none
	 */
	 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;
	 }
}

Observed: implements the standard interface class library SplObserver. An update method.

<?php

class TSPLObserver implements SplObserver{
	 public function update(SplSubject $subject){
		 echo &#39;The new state of subject &#39; , nl2br("\r\n");
//		 echo &#39;The new state of subject &#39;.$subject->getValue();
	 }
}
<?php

class TSPLObserver1 implements SplObserver{
	 public function update(SplSubject $subject){
		 echo &#39;The new state of subject one &#39; , nl2br("\r\n");
//		 echo &#39;The new state of subject &#39;.$subject->getValue();
	 }
}


Test call (in the same directory):

<?php

function __autoload($classname) { 
  require_once ($classname . ".php"); 
}

$subject = new TSPLSubject();
$subject->attach(new TSPLObserver());
$observer1 = new TSPLObserver1();
$subject->attach($observer1);
//$subject->attach(new TSPLObserver2());
//$subject->detach($observer1);

$subject->notify();

exit();

Output:

>php basic.php
The new state of subject

The new state of subject one

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/875469.htmlTechArticlePHP5+ standard function library observer to implement PHP’s observer design pattern is relatively simple to implement, but it is already available in the PHP5 version Standard library class library support, we only need to simply inherit and implement...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn