Implementation of PHP event mechanism

*文
Release: 2023-03-18 14:56:02
Original
1726 people have browsed it

How does PHP implement event mechanism? This article mainly introduces the method of implementing event mechanism in PHP, and analyzes the principle of event mechanism and related implementation of PHP with examples. I hope to be helpful.

The specific analysis is as follows:

There are not many languages ​​with built-in event mechanisms, and PHP does not provide such a function. To put it simply, an event is an Observer pattern, which is easy to implement. But the difference is that anyone can add an event listener, but it can only be triggered by the object that directly contains it. This is a little bit difficult. PHP has a debug_backtrace function, which can get the current call stack. From this, you can find a way to determine whether the object that calls the event triggering function directly contains its object.


<?php
/**
* 事件
*
* @author xiezhenye <xiezhenye@gmail.com>
*/
class Event {
  private $callbacks = array();
  private $holder;
  function __construct() {
    $bt = debug_backtrace();
    if (count($bt) < 2) {
      $this->holder = null;
      return;
    }
    $this->holder = &$bt[1][&#39;object&#39;];
  }
  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][&#39;object&#39;] !== $this->holder)
        || (count($bt) < 2))) {
      throw(new Exception(&#39;Notify can only be called in holder&#39;));
    }
    foreach ($this->callbacks as $callback) {
      $args = func_get_args();
      call_user_func_array($callback, $args);
    }
  }
}
Copy after login

Related recommendations:

##Example analysis of how to implement simulated http requests in PHP

Detailed explanation of PHP socket push technology

##PHPCrawl crawler library implements grabbing Kugou song list

The above is the detailed content of Implementation of PHP event mechanism. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!