This article mainly introduces the PHP to the event mechanism definition and the realization, the interest friend's reference, hoped to have the help to everybody.
This article describes the PHP implementation of the event mechanism of the method, specifically as follows:
<?php/*** Events */class Event {Private $callbacks = array (); private $holder; function __construct () {$BT = Debug_backtr Ace (); if (count ($BT) < 2) {$this->holder = null; Return } $this->holder = & $BT [1][' object ']; } function Attach () {$args = Func_get_args (); Switch (count ($args)) {Case 1:if (is_callable ($args [0])) {$this->callbacks[]= $args [0]; Return } break; Case 2:if (is_object ($args [0]) && is_string ($args [1])) {$this->callbacks[]= array (& $args [0], $args [1]); } return; Default:return; }} function Notify () {$BT = Debug_backtrace (); if ($this->holder && (count ($BT) >= 2 && $BT [Count ($BT)-1][' object ']!== $this->holder) || (Count ($BT) < 2))) {Throw (New Exception (' Notify can only is called in Holder ')); } foreach ($this->callbacks as $callback) {$args = Func_get_args (); Call_user_func_array ($callback, $args); } }}
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Based on jquery, PHP and MySQL achieve a rating function
Application of PHP variables, arrays, regular expressions and templates
How to get the first letter of Chinese pinyin in PHP program