php 中的魔术方法-----“事件方法”
来源:http://lornajane.net/posts/2012/phps-magic-__invoke-method-and-the-callable-typehint
php 中的这个对象 ,有时间研究一下:

PHP中会有一些魔术方法:PHP 将所有以 __(两个下划线)开头的类方法保留为魔术方法。所以在定义类方法时,除了上述魔术方法,建议不要以 __ 为前缀。
魔术方法有:__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(),__wakeup(), __toString(), __invoke(), __set_state(), __clone() 和 __debugInfo() 等方法在 PHP 中被称为"魔术方法"(Magic methods)。在命名自己的类方法时不能使用这些方法名,除非是想使用其魔术功能。
Typehint
PHP has a variety of magic methods; methods named with two underscores at the start, which get called automatically when a particular event happens.(所以我标题中也叫事件方法,指特定的时间触发时,会自动调用,自动执行的方法。) In PHP 5.3, a new magic method was added: __invoke().
__invoke()
The __invoke() method gets called when the object is called as a function. When you declare it, you say which arguments it should expect. Here's a trivially simple example:
class Butterfly {
public function __invoke() {
echo "flutter";
}
}
We can instantiate a Butterfly object, and then just use it like a function:
$bob = new Butterfly();
$bob(); // flutter
If you try to do the same thing on an object without an __invoke() method, you'll see this error:
PHP Fatal error: Function name must be a string in filename.php on line X
We can check if the object knows how to be called by using the is_callable() function.
Callable Typehint
In PHP 5.4 (the newest version, and it has lots of shiny features), we have the Callable typehint. This allows us to check whether a thing is callable, either because it's a closure, an invokable object, or some other valid callback. Another trivial example to continue the butterflies and kittens theme:
也就是说, php 中 callable类型一共有3种: 
function sparkles(Callable $func) {
$func();
return "fairy dust";
}
class Butterfly {
public function __invoke() {
echo "flutter";
}
}
$bob = new Butterfly();
echo sparkles($bob); // flutterfairy dust
So there it is, one invokable object being passed into a function and successfully passing a Callable typehint. I realise I also promised kittens, so here's a cute silver tabby I met the other day:
php 中的魔术方法-----“事件方法”的更多相关文章
- day03—JavaScript中DOM的Event事件方法
转行学开发,代码100天——2018-03-19 1.Event 对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用, ...
- delphi 常用属性+方法+事件+代码+函数
内容居中(属性) alignment->tacenter mome控件 禁用最大化(属性) 窗体-> BorderIcons属性-> biMaximize-> False 让鼠 ...
- jQuery 事件 方法
jQuery 事件方法 事件方法触发器或添加一个函数到被选元素的事件处理程序. 下面的表格列出了所有用于处理事件的 jQuery 方法. 方法 描述 bind() 向元素添加事件处理程序 blur() ...
- Jq_DOM元素方法跟JQuery 核心函数跟JQuery 事件方法
JQuery DOM 元素 函数 描述 .get() 从队列中删除所有未运行的项目. .ind ...
- jQuery 的58种事件方法你都用过了吗
jQuery 事件方法 事件方法触发或将函数附加到所选元素的事件处理程序. 下表列出了用于处理事件的所有jQuery方法. 方法 描述 bind() 在3.0版中已弃用. 请改用on()方法.将事件处 ...
- PHP 类中的魔术方法
定义: PHP类中以两个下画线“__”开头的方法被称为魔术方法. 分类: 例如:构造方法:__construct:析构方法:__destruct:动态重载:__set().__get().__call ...
- php中的魔术方法
__construct 构造器是一个魔术方法,当对象被实例化时它会被调用.在一个类声明时它常常是第一件做的事但是没得必要他也像其他任何方法在类中任何地方都可以声明,构造器也能像其他方法样继承.如果我们 ...
- 给ul中的li添加事件的多种方法
给ul中的li添加事件的多种方法 这是一个常见,而且典型的前端面试题 <ul> <li>11111</li> <li>22222</li> ...
- jquery中交替点击事件toggle方法的使用示例
jquery中交替点击事件toggle方法中有两个参数,分别是要交替执行的事件.如果不传参默认是显示隐藏功能,下面有个不错的示例,感兴趣的朋友可以参考下 复制代码代码如下: $('#clickId‘) ...
随机推荐
- Drivers Dissatisfaction
Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...
- Android 项目开发
可以使用mapview.getMapCenter()获取当前可视范围中心点的坐标,然后计算出数据库中的点与中心点的距离值,如果该距离在触发显示的范围内(比如100米),就显示该点到地图上.百度地图的S ...
- Spring Boot 系列教程7-EasyUI-datagrid
jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...
- XAMPP(v1.83)中的PHP(v5.5.15)访问SQLServer2014
驱动安装: 1. 下载SQLServer的微软官方PHP驱动,http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 2. 安装SQLSRV31 ...
- 如何延长zencart1.5后台的登录时间而不退出
2012-12-25 使用过zencart1.5版本的人都知道,后台登陆后,如果没有任何操作的话最长15分钟后就自动退出,这个对于后台管理是比较麻烦的.这个是zencart1.5在安全性上做的一个改进 ...
- hdu_2457_DNA repair(AC自动机+DP)
题目连接:hdu_2457_DNA repair 题意: 给你N个字符串,最后再给你一个要匹配的串,问你最少修改多少次,使得这个串不出现之前给的N的字符串 题解: 刚学AC自动机,切这题还真不知道怎么 ...
- FusionCharts使用问题及解决方法(五)-FusionCharts常见问题大全
在前4篇文章中,我们总结了FusionCharts XT图表使用中的一些常见问题(FAQ)及解决方法,本文继续讨论FusionCharts使用者常见的一些复杂报错及错误的调试/解决方法. 问题描述:是 ...
- ural1772 Ski-Trails for Robots
Ski-Trails for Robots Time limit: 1.0 secondMemory limit: 64 MB One of the stages of the Robot Cross ...
- android Service Activity三种交互方式(付源码)(转)
android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread android Service Binder交互通信实例 最下边有源代码: ...
- FZU Problem 2028 时空门问题(DFS+优化)
一开始是MLE,后来想到了用vector,化二维为一维,做了这一步优化后,这就是很基础的一个广搜了 #include<iostream> #include<cstdio> #i ...