PHP 利用闭包偷窥马对人类的想法
2024-08-30 01:20:25
<?php /**
* reference:http://www.php.net/manual/en/reflectionmethod.getclosure.php
* Learn this and you will know How to peer through what a horse is thinking!
*
* Class Client
*/
class People
{
private $heartWords = "How beautiful the horse is!I want to ride it!"; public function hook()
{
$who = get_class($this);
echo ucfirst($who). " says :".$this->heartWords. "\n";
} public function peer()
{
return function(){
$this->hook();
};
}
} class Horse
{
private $heartWords = "How silly the guy is! A big ass!";
} $people = new People();
$horse = new Horse(); try{
$reflection = new ReflectionClass(get_class($people));
}
catch(ReflectionException $e)
{
echo $e->getMessage() ."\n";
return;
} $closure = $reflection->getMethod('hook')->getClosure($people);
$truth = $closure->bindTo($horse,$horse); $truth(); // Horse says :How silly the guy is! A big ass!
最新文章
- 仿喜马拉雅实现ListView添加头布局和脚布局
- Best Coder Round#25 1003 树的非递归访问
- xCode删除storyboard,新建window并启动
- python中的namespace
- css居中的几种方式
- javascript-实现日期大写
- 关于 Java 性能监控您不知道的 5 件事,第 1 部分
- Callgrind 使用 2
- 标准I/O的替代软件
- Python之路【第一篇】:Python前世今生
- .NET 中使用 HttpWebResponse 时 Cookie 的读取
- 手工删除crfclust.bdb文件
- 微信小程序入门指南
- sql执行内部操作期间检测到不一致性解决方案
- MySQL数据库(一)
- SpringBoot 基础01
- Qt QLineEdit
- 【Tomcat】tomcat内存配置登记册
- Ubuntu安装软件提示boot空间不足
- 详细解读Volley(一)—— 基本Request对象 &; RequestQueue