dpgui8229808 2012-05-02 00:20
浏览 83
已采纳

通过__get()魔术方法访问属性/方法

how can i access the second property or method in this statement

$this->test->hello();

In my __get() I can only figure out how to figure out what the test property is. I want to be also be able to capture the 'hello' method call. and do some dynamic things with it.

So in short if I type

$this->test->hello()

I want to echo each segment

echo $propert // test
echo $method //hello

The issue is that my test is being used to instantiate a new class object from an outside class. The method hello belongs to the test class object.

I want to capture the method within my __get().

How can i do this?

EDIT:

public function __get($name)
        {
            if ($name == 'system' || $name == 'sys') {

                $_class = 'System_Helper';

            } else {

                foreach (get_object_vars($this) as $_property => $_value) {

                    if ($name == $_property)
                        $_class = $name;
                }
            }

            $classname = '\\System\\' . ucfirst($_class);
            $this->$_class = new $classname();

            //$rClass = new \ReflectionClass($this->$_class);
            $rClass = get_class_methods($this->$_class);
            foreach($rClass as $k => $v)
            echo $v."
";
            //print_r($rClass);

            return $this->$_class;
  • 写回答

3条回答 默认 最新

  • duangangpin078794 2012-05-02 00:35
    关注

    It seems you are after some kind of proxy class, this might suit your needs.

    class ObjectProxy {
        public $object;
    
        public function __construct($object) {
            $this->object = $object;
        }
    
        public function __get($name) {
            if (!property_exists($this->object, $name)) {
                return "Error: property ($name) does not exist";
            }
            return $this->object->$name;
        }
    
        public function __call($name, $args) {
            if (!method_exists($this->object, $name)) {
                return "Error: method ($name) does not exist";
            }
            return call_user_func_array(array($this->object, $name), $args);
        }
    }
    
    class A {
        public $prop = 'Some prop';
    
        public function hello() {
            return 'Hello, world!';
        }
    }
    
    class B {
        public function __get($name) {
            if (!isset($this->$name)) {
                $class_name = ucfirst($name);
                $this->$name = new ObjectProxy(new $class_name);
            }
            return $this->$name;
        }
    }
    $b = new B();
    var_dump($b->a->hello());
    var_dump($b->a->prop);
    var_dump($b->a->foo);
    var_dump($b->a->bar());
    

    Output:

    string 'Hello, world!' (length=13)
    string 'Some prop' (length=9)
    string 'Error: property (foo) does not exist' (length=36)
    string 'Error: method (bar) does not exist' (length=34)
    

    Example:

    http://ideone.com/dMna6

    It could be easily extend for other magic methods like __set, __callStatic, __isset, __invoke, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改