Example Analysis of PHP Implementation of Event Mechanism_PHP Tutorial

WBOY
Release: 2016-07-13 09:48:29
Original
772 people have browsed it

Analysis of Examples of PHP Implementing Event Mechanism

This example describes how PHP implements event mechanism. Share it with everyone for your reference. The specific analysis is as follows:

There are not many languages ​​with built-in event mechanism, 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.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

/**

* 事件

*

* @author xiezhenye

*/

class Event {

private $callbacks = array();

private $holder;

function __construct() {

$bt = debug_backtrace();

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 be called in holder'));

}

foreach ($this->callbacks as $callback) {

$args = func_get_args();

call_user_func_array($callback, $args);

}

}

}

1

2 3

45 6 7 8 9 10
11
12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<🎜>/**<🎜> <🎜>* Event<🎜> <🎜>*<🎜> <🎜>* @author xiezhenye */ class Event { private $callbacks = array(); private $holder; function __construct() { $bt = debug_backtrace(); 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 be called in holder'));<🎜> <🎜>}<🎜> <🎜>foreach ($this->callbacks as $callback) { $args = func_get_args(); call_user_func_array($callback, $args); } } }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1022668.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022668.htmlTechArticleAnalysis of Examples of PHP Implementing Event Mechanism This example describes how PHP implements event mechanism. Share it with everyone for your reference. The specific analysis is as follows: There are not many languages ​​​​with built-in event mechanisms,...
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