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‘) ...
随机推荐
- 解读QML之三
QML语法 1.QML基本语法 1.1导入声明 导入声明允许客户端告诉QML引擎可以在QML文档中使用哪些模块,JavaScript资源以及组件目录.文档中可以使用的类型依赖于在文档中导入的模块.资源 ...
- linux c中select使用技巧
1.select函数作为定时器使用 it_value.tv_sec = 0; it_value.tv_usec = 100000: select(1,NULL,NULL,NULL,& ...
- addEventListener与removeEventListener
addEventListener:添加事件监听器 element.addEventListener(event, function, useCapture) event:事件类型,字符串,不要加&qu ...
- apicloud教程2 (转载)
本帖最后由 中山赢友网络科技有限公司 于 2015-10-17 15:38 编辑 继<APICloud之小白图解教程系列(一):认识APICloud>之后的第二篇教程. 本篇教程有以下知识 ...
- Slow HTTP Denial of Service Attack 漏洞解决
修改tomcat conf 下 server.xml 文件 <Connector port="8080" protocol="HTTP/1.1" con ...
- libmysql.dll 找不到
在用C#开发的时候,需要连接MySQL ,系统提示 libmysql.dll 找不到模块. 我们可以找到 MySQL安装文件夹下的 C:\Program Files\MySQL\MySQL Ser ...
- Swift 使用Extension 场景 浅析
别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下 ...
- linux内核移植到S5pv210
make s5pv210_defconfig 1.System Type ---> (0) S3C UART to use for low-level messages 2.Kernel ha ...
- db2数据导出导入
C:\Users\yexuxia>set db2instance=TCASHMAN C:\Users\yexuxia>db2(c) Copyright IBM Corporation 19 ...
- Listview和checkbox多选
在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上看上去只是改变checkbox那么简单,然而实际开发中,实现起来并不是那么得心应手.尤其当 ...