如何在类实例内部取得实例名? 例:$example=new Datagrid();要取得值为:example有个魔法变量为 __CLASS__ ,可以返回 Datagrid。如果想要返回 example呢?有什么方式啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class Datagrid { function get_host() { $a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__.'";')); print_r($a); }}$example = new Datagrid();$example->get_host(); array_filter 函数需要一个回调函数,以决定删选的范围create_function('$v', 'return get_class($v)=="'.__CLASS__.'";')展开来就是function func($v) { return get_class($v) == '你需要检查的值';} <?php class Datagrid { function get_host() { $a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__.'";')); print_r($a); } } $example = new Datagrid(); $example2 = new Datagrid(); #$example->get_host(); $example2->get_host();?>这个不对吧,如果全部变量中有2个这个类的实例,将全部输出,而且必须声明 gloabl $example;因为在函数中不是全局变量的. 不过老大的思路真的挺广的.学习了输出结果为:Array( [example] => Datagrid Object ( ) [example2] => Datagrid Object ( )) <?php class Datagrid { function get_host() { $this->id= microtime(); $a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__."\"&&\$v->id=='{$this->id}';")); print_r($a); } } $example = new Datagrid(); $example2 = new Datagrid(); #$example->get_host(); $example2->get_host();?>哈哈,,按照唠叨的思路写了个,,其实上面的->id 换成唯一的id就更好啦,忘记php的那个函数了 求PHP获取客户端IP地址与MAC实际地址的代码 新手问个绑定数据的问题 php源码IE下乱码,Firefox不是乱码,请大家帮忙看看。 PHP接口的一个小问题 smarty日期转换有知道的吗? 为什么很简单的文件操作也出错? 文件上傳後! 我怎麼得到它原來的文件類型呀(就是擴展名呀!) PHP 页面不能与数据库同步刷新的问题 在PHP中使用InterBase6.5作数据后台,在写论坛中分页显示是怎么处理的? 学校一周菜谱怎么来写? php里一个关于循环的问题 解读代码
function get_host() {
$a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__.'";'));
print_r($a);
}
}
$example = new Datagrid();
$example->get_host();
展开来就是function func($v) {
return get_class($v) == '你需要检查的值';
}
<?php
class Datagrid {
function get_host() {
$a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__.'";'));
print_r($a);
}
}
$example = new Datagrid();
$example2 = new Datagrid();
#$example->get_host();
$example2->get_host();
?>这个不对吧,如果全部变量中有2个这个类的实例,将全部输出,而且必须声明 gloabl $example;
因为在函数中不是全局变量的. 不过老大的思路真的挺广的.学习了
输出结果为:
Array
(
[example] => Datagrid Object
(
) [example2] => Datagrid Object
(
))
class Datagrid {
function get_host() {
$this->id= microtime();
$a = array_filter($GLOBALS, create_function('$v', 'return get_class($v)=="'.__CLASS__."\"&&\$v->id=='{$this->id}';"));
print_r($a);
}
}
$example = new Datagrid();
$example2 = new Datagrid();
#$example->get_host();
$example2->get_host();
?>
哈哈,,按照唠叨的思路写了个,,其实上面的->id 换成唯一的id就更好啦,忘记php的那个函数了