__tostring和__invoke 方法
首先放上代码:
<?php
class MagicTest{
//__tostring会在把对象转换为string的时候自动调用
public function __tostring() {
return "This is the class MagicTest";
}
//__invoke会在把对象当作一个方法调用的时候自动调用
public function __invoke($x) {
echo "__invoke called with parameter".$x."<br/>";
}
} $obj = new MagicTest();
echo $obj."<br/>";
$obj(5);
输出为:
This is the class MagicTest
__invoke called with parameter5
可以看到调用$obj的时候,自动调用 __tostring() 里面的字符,这时,如果你把tostring前面的双下划线减成一条,则会出现错误
同样在调用 $obj(5) 的时候,会自动调用 __invoke 方法
__tostring和__invoke 方法的更多相关文章
- __tostring()和__invoke()的用法
PHP有很多内置的魔术方法,这里我们聊哈tostring和involk吧. __tostring()魔术方法 将一个对象当做一个字符串来使用时,会自动调用该方法,并且在该方法中,可以返回一定的字符串, ...
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...
- PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...
- PHP常用魔术方法(__invoke魔术方法)
<?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload_register('\\ ...
- PHP魔术方法之__invoke()
将对象当作函数来使用时,会自动调用该方法. class ShowProfile extends Controller { public function __invoke($id) { return ...
- PHP基础知识之魔术方法
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sle ...
- 前端学PHP之面向对象系列第二篇——魔术方法
× 目录 [1]构造方法 [2]析构方法 [3]不可访问属性[4]对象复制[5]字符串[6]对象不存在[7]自动加载类[8]串行化[9]函数调用 前面的话 php在面向对象部分有很多相关的魔术方法,这 ...
- PHP中的魔术方法(2)
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...
- php魔术方法使用场景
php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...
随机推荐
- 解决Navicat连接mysql报错:1862 - Your password has expired. To log in you must change it using a client that supports expired passwords.
今天尝试用Navicat连接mysql时,发现一个1862的报错问题: 后来参照这篇文章https://blog.csdn.net/u010513756/article/details/5073501 ...
- nginx基本参数详解
运行用户 user nobody; 启动进程,通常设置成和cpu的数量相等 worker_processes 1; 全局错误日志及PID文件 error_log logs/error.log; err ...
- iOS-使用ALAssetsLibrary获取相册图片视频
用ALAssetsLibrary获取相册图片视频 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library enumera ...
- Eclipse中Maven的本地仓库引导配置
简单整理一下,方便理解操作. 1.本地拷贝maven文件后,打开maven中的.setting 文件: 2.配置文件: <?xml version="1.0" encodin ...
- java调接口
package util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputSt ...
- 201621123014《Java程序设计》第十一周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...
- 20165210 Java第三次实验报告
20165210 实验二 敏捷开发与XP实践 一.敏捷开发与XP实践-1 实验要求: http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替 ...
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- Jenkins之构建触发器配置(转载)
构建触发器配置,当你在文本框中输入配置的时间后,文本框下方会有时间解释,这样可以很好的看到自己配置的时间对不对. 可以清晰看到我的配置第一个运行时间是周五上午10点执行,第二次是星期六上午10点. ...
- Wireshark图解教程(简介、抓包、过滤器)【转载】
原文网址:http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据, ...