如何在类实例内部取得实例名? 例:$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文章采集类 这个怎么样? 程序错误 setcookie的问题 安装xoops后,当要登录时,出现错误,请解决(在线等,解决马上给分) 我想用PHP画曲线图,扁图之类的图,请问用什么工具??高分 擂台:各位高手编写猜数字这个小游戏,谁的构思巧妙代码简练就算赢 为什么打印出来的没有背景颜色呢? mysql 查询结果的问题,求帮忙 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的那个函数了