Home > Backend Development > PHP Tutorial > 倾覆常识 - 死循环结构代码,也有机会执行循环外的逻辑

倾覆常识 - 死循环结构代码,也有机会执行循环外的逻辑

WBOY
Release: 2016-06-13 12:47:50
Original
932 people have browsed it

颠覆常识 -- 死循环结构代码,也有机会执行循环外的逻辑
 
class ZookeeperDemo extends Zookeeper {
 
  public function watcher( $i, $type, $key ) {
    echo "Insider Watcher\n";
 
    // Watcher gets consumed so we need to set a new one
    $this->get( '/test', array($this, 'watcher' ) );
  }
 
}
 
$zoo = new ZookeeperDemo('127.0.0.1:2181');
$zoo->get( '/test', array($zoo, 'watcher' ) );
 
while( true ) {
  echo '.';
  sleep(2);
}


现在运行该脚本。

$ php zookeeperdemo1.php
此处应该会每隔2秒产生一个点。现在切换到ZooKeeper客户端,并更新“/test”值。

[zk: 127.0.0.1:2181(CONNECTED) 20] set /test foo
这样就会静默触发PHP脚本中的“Insider Watcher”消息。


怎么会这样的?进入while结构后,为什么还能执行watcher方法。请各位看官发表讨论。


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template