php中var_dump() 打印出一个对象的时候,信息怎么看?
php 的一个依赖注入容器, 说白了,就是用php 的反射类,来在运行的时候动态的分析类具有的函数,以及动态分析函数的参数, 从而实例化类,并执行类的方法。
另外,php 中的 typehint 还是很有用的,反射的时候就有用到!





下面贴出代码:
<?php
class Bim
{
public function doSomething()
{
echo __METHOD__, '|';
}
} class Bar
{
private $bim; public function __construct(Bim $bim)
{
$this->bim = $bim;
} public function doSomething()
{
$this->bim->doSomething();
echo __METHOD__, '|';
}
} class Foo
{
private $bar; public function __construct(Bar $bar)
{
$this->bar = $bar;
} public function doSomething()
{
$this->bar->doSomething();
echo __METHOD__;
}
} class Container
{
private $s = array(); public function __set($k, $c)
{
$this->s[$k] = $c;
} public function __get($k)
{
// return $this->s[$k]($this);
return $this->build($this->s[$k]);
} /**
* 自动绑定(Autowiring)自动解析(Automatic Resolution)
*
* @param string $className
* @return object
* @throws Exception
*/
public function build($className)
{
echo "<pre>";var_dump($className);
// 如果是匿名函数(Anonymous functions),也叫闭包函数(closures)
if ($className instanceof Closure) {
// 执行闭包函数,并将结果
return $className($this);
} /** @var ReflectionClass $reflector */
$reflector = new ReflectionClass($className); // 检查类是否可实例化, 排除抽象类abstract和对象接口interface
if (!$reflector->isInstantiable()) {
throw new Exception("Can't instantiate this.");
} /** @var ReflectionMethod $constructor 获取类的构造函数 */
$constructor = $reflector->getConstructor(); // 若无构造函数,直接实例化并返回
if (is_null($constructor)) {
return new $className;
} // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表
$parameters = $constructor->getParameters(); // 递归解析构造函数的参数
$dependencies = $this->getDependencies($parameters); // 创建一个类的新实例,给出的参数将传递到类的构造函数。
return $reflector->newInstanceArgs($dependencies);
} /**
* @param array $parameters
* @return array
* @throws Exception
*/
public function getDependencies($parameters)
{
$dependencies = []; /** @var ReflectionParameter $parameter */
foreach ($parameters as $parameter) {
/** @var ReflectionClass $dependency */
$dependency = $parameter->getClass();
if (is_null($dependency)) {
// 是变量,有默认值则设置默认值
$dependencies[] = $this->resolveNonClass($parameter);
} else {
// 是一个类,递归解析
$dependencies[] = $this->build($dependency->name);
}
} return $dependencies;
} /**
* @param ReflectionParameter $parameter
* @return mixed
* @throws Exception
*/
public function resolveNonClass($parameter)
{
// 有默认值则返回默认值
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
} throw new Exception('I have no idea what to do here.');
}
} // ----
//$c = new Container();
//$c->bar = 'Bar';
//$c->foo = function ($c) {
// return new Foo($c->bar);
//};
//// 从容器中取得Foo
//$foo = $c->foo;
//$foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething ///*// ----
$di = new Container();
//
$di->foo = 'Foo';
//
///** @var Foo $foo */
$foo = $di->foo;
//
var_dump($foo);exit;
echo "<pre>";var_dump($foo->doSomething()); var_dump( get_class($foo));
die();
///*
//Foo#10 (1) {
// private $bar =>
// class Bar#14 (1) {
// private $bim =>
// class Bim#16 (0) {
// }
// }
//}
//*/
//
$foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething*/
php中var_dump() 打印出一个对象的时候,信息怎么看?的更多相关文章
- log日志中不打印异常栈的具体信息
问题与分析 最近在查项目的log时发现报了大量的NPE(NullPointerException),诡异的是只log了Exception的类名,却没有具体的堆栈信息,以致于无法对该NPE异常进行准确定 ...
- 飘逸的python - 用urlparse从url中抽离出想要的信息
最近有个需求,要检测配置中的那些url的域名是否都正常,即是否都能ping通. 不过配置中url格式是这样的 http://www.something.com:1234/ . 要ping的是www.s ...
- 用C语言打印出三角函数
在网上看到一个实例,是用C 中的* 打印出三角函数cos #include<stdio.h> #include<math.h> int main() { double y; i ...
- 在C++中打印出变量的方法
在C++中只能显示出字符串,而如果要想打印出其他类型的变量,则只能将其先转换为字符串类型. 例如:想打印出int型变量value的值 int value; 则需: char str[1];//定义一 ...
- Xcode真机调试iOS10中Nslog 打印不出东西
Xcode真机调试iOS10中Nslog 打印不出东西 解决方案 通过以下途径找到 Product->Scheme->EditScheme ios9以前的 如果不加 1 的那句 在xcod ...
- java算法面试题:从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 ;读取docx 读取doc 使用poi 相关jar包提集提供下载
从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 1,张三,28 2,李四,35 3,张三,28 4,王五,35 5,张三,28 6,李四,35 7,赵六,28 ...
- java-得到字符串中出现次数最最多的字符,并打印出字符以及出现次数
最近面试总被面试到,整理出几种方式(有参考别人的部分) /** * java一个字符串中出现次数最多的字符以及次数 * @param args */ public static void main(S ...
- 剑指offer27:按字典序打印出该字符串中字符的所有排列
1 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: ...
- C语言:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,-主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分-使用插入排序法对字符串中的字符进行升序排序。-从文件中找到指定学号的学生数据,读入次学生数据,
//根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小与形参c一致,数量由形参d指定.例如:输入c为Y,d为4,则输出ZABC. #include <stdio.h> ...
随机推荐
- wpa_supplicant_8_ti hostapd wpa_supplicant TI 官方的wpa_supplicant hostapd 移植到linux
在移植 wpa_supplicant_8_ti 的时候碰到很多头文件找不到.然后参考了下面的博客 http://blog.csdn.net/penglijiang/article/details/85 ...
- java设计模式案例详解:代理模式
代理模式就是用一个第三者的身份去完成工作,其实际意义跟字面意思其实是一样的,理解方式有很多,还是例子直观. 本例的实现类是实现买票功能,实际应用想要添加身份验证功能,利用代理模式添加验证步骤.上例子: ...
- EGL接口介绍-----Android OpenGL ES底层开发
引自:http://www.cnitblog.com/zouzheng/archive/2011/05/30/74326.html EGL 是 OpenGL ES 和底层 Native 平台视窗系统之 ...
- lucene-SpanFirstQuery 和SpanNearQuery 跨度查询
1.SpanFirstQuery查询 对出现在一个域中前n个位置的跨度查询. public void testSpanFirstQuery() throws Exception{ SpanzFirts ...
- Ubuntu + Django + Nginx + uwsgi
环境 Ubuntu 14.04 Python 2.7 Django 1.8.4 1 安装Nginx sudo apt-get install nginx 测试 sudo /etc/init. ...
- POJ - 3666 Making the Grade(dp+离散化)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- ormlite 多表联合查询
ormlite 多表联合查询 QueryBuilder shopBrandQueryBuilder = shopBrandDao.queryBuilder(); QueryBuilder shopQu ...
- CNS的数据库搜索网站可用solr实现
使用solr的DIH (data import handler) 可以操作后台数据库,前端solr自带的search ui (localhost:8983/solr/collection1/brows ...
- MiniProfiler找不到jquery的
我一直在使用MiniProfiler来衡量网站性能.当我从1.9版本升级到2.0,它停止工作.我改变了命名空间从MvcMiniProfiler StackExchange.Profiling的.但是, ...
- Ubuntu Linux系统下的SVN客户端工具PySVN
在Windows下面一直在用TortoiseSVN做为SVN客户端工具,但它居然没提供Linux版本,无视Linux用户的存在.它视我如空 气,偶视它如废土.开始探索尝试其他跨平台的SVN客户端,最后 ...