我也知道php和nodejs,在javascript中我们有异步编程,所以我理解其中事件的含义.但是我看到Yii和Zend 2中的事件并且也使用它们,但我可以不明白它的含义,它在 php 中是如何工作的,它到底是做什么的?
事件是带有名称的回调函数的抽象.通常,我们将它们定义为 $eventName =>$listener
,其中 $listener
是 $eventName
再一次 - 要记住的核心点是事件是回调函数.仅此而已.
它们之间的唯一区别是我们如何调用它们.
<块引用><块引用>一个事件是在引导步骤中使用其所需参数定义的,但在没有参数的情况下按需调用.而回调函数是使用参数调用的,并且仅
考虑这个例子,
attach('my_event', function(){print_r(func_get_args());}, array('foo', 'bar'));
因为我们刚刚定义了一个事件,所以我们会像这样调用,
$eventManager->trigger('my_event');
这将输出:Array([0] => [foo], [1] => [bar]
由于我们大多数人都熟悉 JavaScript 偶数驱动架构,因此无需提及其常见用法的示例:
var a = document.getElementsByTagName('a')[0];a.onclick = function(event) {//<-- 我们用 event 参数定义一个事件event.preventDefault();alert('点击了一个元素');}a.点击();//<-- 但我们调用它时不带参数//或者如果你想要一个 Jquery$("a").click(函数(事件){event.preventDefault();alert('点击了一个元素');});$("a").click();
由于在 PHP 中我们没有这种事件驱动的性质,我们可以将其替换为我们自己的管理事件并充分利用它的类.
虽然事件让很多人感到困惑,但它们非常有用.
假设您有一个内容管理系统 (CMS),您的用户可以在其中决定如何处理 404
错误.说,他们可以处理
1) 显示空白页
2) 重定向到 /
3) 显示自定义消息
如果没有事件,您将不得不这样做,例如
if ($router->isMatched($request)){//进行调度等} 别的 {//这里开始处理 404 错误switch($config->read('404_way_handle')){案例show_blank":死();休息;案例show_msg":echo '一些自定义消息';休息;案例重定向"://做重定向休息;}}
通过事件,您可以简化可读性并使代码更易于维护:
if ($router->isMatched($request)){//进行调度} 别的 {$eventManager->trigger('404_handler');}
而 404_handler
本身看起来像
$eventManager->attach('404_handler', function(){switch($config->read('404_way_handle')){案例show_blank":死();休息;案例show_msg":echo '一些自定义消息';休息;案例重定向"://做重定向休息;}}, $配置);
1) 事件提高了可读性,这对未来很有好处
2) 事件确实遵守单一职责原则,因为您可以简单地将 $eventManager
注入到需要它的类中,而回调函数可能会破坏它或也可以引入全局状态(这对单元测试不利).
3) 有不同类型的逻辑 - 模板逻辑、业务逻辑、错误处理程序逻辑、数据访问逻辑等.事件通过解耦业务(或其他类型)逻辑简化您的应用程序逻辑从它的配置逻辑,让你最终得到清晰的应用程序逻辑.
<块引用><块引用>如果您想了解它们在 Zend 中的工作方式,可以观看此讲座框架 2(即使您不熟悉 Zend 框架 2 也可以观看)
MVC 相关架构中的事件
既然您一直在谈论框架,那么在 MVC 相关
架构中也可能存在事件,这不值得一提.由于事件是回调函数,因此您可以在类似 MVC 的架构中抽象出常见的 boostrap
事件,就像这样.
$mvcEvent->on(MVC_EVENT::ROUTE_MATCH, function(){$mvcEvent->on(MVC_EVENT::DISTPATCH, function($content){echo $mvcEvent->trigger(MVC_EVENT::RENDER, $content);});});
注意:在纯 MVC 理论中,根本没有事件.它们确实起到了帮助者的作用,但同样 - 在框架中,您可以抽象它们并将它们称为事件".
I know php and nodejs too,in javascript we have asynchronize programming ,so I understand meaning of event in it.but I saw Event in Yii and Zend 2 and use them too,but I can't understand the meaning of it,how it works in php and what exactly does in it?
An event is an abstraction for callback functions with their name.
Typically, we'd define them as $eventName => $listener
, where $listener
is a callback function for the $eventName
Again - the core point to remember, is that events are callback functions. Nothing more.
The only difference between them, is how we do invoke them.
An event is defined on bootstrap step with its required arguments, but invoked on demand without arguments. While the callback function is invoked with arguments and only
Consider this example,
<?php
$eventManager = new EventManager();
$eventManager->attach('my_event', function(){
print_r(func_get_args());
}, array('foo', 'bar'));
As we have just defined an event, we'd invoke like,
$eventManager->trigger('my_event');
This will output: Array([0] => [foo], [1] => [bar]
Since most of us are familiar with JavaScript even-driven architecture, its worth nothing to mention an example of its common usage:
var a = document.getElementsByTagName('a')[0];
a.onclick = function(event) { // <-- We define an event with the event argument
event.preventDefault();
alert('A element was clicked');
}
a.click(); // <-- but we invoke it without arguments
// or If you want a Jquery
$("a").click(function(event){
event.preventDefault();
alert('A element was clicked');
});
$("a").click();
Since in PHP we don't have such event-driven nature, we can replace it with our own class that manage events and takes a full advantage of it.
While events confuse so many people, they are extremely useful.
Imagine you have a Content Management System (CMS), where your users can decide how to handle 404
errors. Say, they can handle with
1) Show a blank page
2) Redirect to /
3) Show a custom message
Without events you would have to do it, like
if ($router->isMatched($request)){
//do dispatch etc
} else {
// Here you start handling 404 errors
switch($config->read('404_way_handle')){
case 'show_blank':
die();
break;
case 'show_msg':
echo 'Some custom message';
break;
case 'redirect':
// do redirect
break;
}
}
With an event you can simplify the readability and keep the code more maintainable:
if ($router->isMatched($request)){
// do dispatch
} else {
$eventManager->trigger('404_handler');
}
while 404_handler
itself looks like
$eventManager->attach('404_handler', function(){
switch($config->read('404_way_handle')){
case 'show_blank':
die();
break;
case 'show_msg':
echo 'Some custom message';
break;
case 'redirect':
// do redirect
break;
}
}, $config);
1) Events improve readability, which is great for future
2) Events do adhere to the Single-Responsibility Principle, because you can simply inject $eventManager
to your classes that need it, while callback functions could break it or could introduce a global state too (Which is bad for unit-testings).
3) There are distinct types of logic - template logic, business logic, error handler logic, data access logic etc etc. Events do simplify your application logic by decoupling business (or another kind) logic from its configuration logic, so that you end up with clear application logic.
You can watch this lecture if you want to know how they do work in Zend Framework 2 (watch it even if you're not familiar with Zend Framework 2)
Events in MVC-related architectures
Since you've been talking about frameworks, its worth nothing to mention, that there could be events in MVC-related
architectures too. And since events are callback functions you can abstract common boostrap
events in your MVC-like architecture, like this.
$mvcEvent->on(MVC_EVENT::ROUTE_MATCH, function(){
$mvcEvent->on(MVC_EVENT::DISTPATCH, function($content){
echo $mvcEvent->trigger(MVC_EVENT::RENDER, $content);
});
});
Note : In pure MVC theory, there are no events at all. They do acts as helpers, but again - in frameworks you can abstract them and call them "events".
这篇关于php中事件的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!