The definition and implementation of PHP for event mechanism

Source: Internet
Author: User
Tags php and mysql
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.