例:$example=new Datagrid();
要取得值为:example有个魔法变量为 __CLASS__ ,可以返回 Datagrid。如果想要返回 example呢?有什么方式啊?

解决方案 »

  1.   

    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();
      

  2.   

    array_filter 函数需要一个回调函数,以决定删选的范围create_function('$v', 'return get_class($v)=="'.__CLASS__.'";')
    展开来就是function func($v) {
      return get_class($v) == '你需要检查的值';
    }
      

  3.   


    <?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
            (
            ))
      

  4.   

    <?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的那个函数了