0

如何确定在哪个类中启动了对变量的引用(并且当前存在)?

例子:

<?php
    class MyClass {
        public $array = array(
            "this",
            "is",
            "an",
            "array"
        );
    }
    
    $class = new MyClass();
    $arrayReference = &$class->array;
    GetClassForVariable($arrayReference); //Should return "MyClass"
?>

我最好的选择是某种Reflection,但我还没有找到任何似乎适合于此的功能。

编辑:

一个更适合我想要的示例如下:

<?php
    class API_Module {
        public $module;
        public $name;
        private $methods = array();
        public function __construct($module, $name) {   
            $this->module = $module;
            $this->name = $name;
            $this->methods["login"] = new API_Method($this, "login", "Login");
        }
        public function GetMethod($method) {
            return $this->methods[$method];
        }
        public function GetURL() {
            return $this->module; //Should return "session"
        }
    }
    class API_Method {
        public $method;
        public $name;
        private $parentReference;
        private $variables = array();
        public function __construct(&$parentReference, $method, $name) {
            $this->parentReference = $parentReference;
            $this->method = $method;
            $this->name = $name;
            $this->variables["myvar"] = new API_Variable($this, "myvar");
        }
        public function GetURL() {
            return $this->GetParentURL() . "/" . $this->method; //Should return "session/login"
        }
        public function GetVariable($variableName) {
            return $this->variables[$variableName];
        }
        private function GetParentURL() {
            // Need to reference the class parent here
            return $this->parentReference->GetURL();
        }
    }
    class API_Variable {
        public $name;
        private $parentReference;
        public function __construct(&$parentReference, $name) {
            $this->parentReference = $parentReference;
            $this->name = $name;
        }
        public function GetURL() {
            return $this->GetParentURL() . "/" . $this->name; //Should return "session/login/myvar"
        }
        private function GetParentURL() {
            // Need to reference the class parent here
            return $this->parentReference->GetURL();
        }
    }
    
    $sessionModule = new API_Module("session", "Session");
    var_dump($sessionModule->GetMethod("login")->GetVariable("myvar")->GetURL()); //Should return "session/login/myvar"
?>

现在,这很好用,但我希望能够在使用$parentReference每个子变量的情况下做到这一点。这可能是不可能的,但我很想知道它是否是。

4

2 回答 2

1

对于您的示例:

$class = new MyClass();
$arrayReference = &$class->array;
GetClassForVariable($arrayReference); //Should return "MyClass"

找出别名最初$arrayReference引用的变量在 PHP 中是不可能的。没有可用的函数解析别名。

此外$class->array,它本身只是一个变量。因此,您还需要根据定义它的类的来找出。这也是不可能的,类似于 PHP 不提供任何东西来解析变量别名,它也不提供任何东西来了解变量的定义。

所以简而言之,PHP 没有ReflectionVariable可用的类;)我想知道它是否可能。

于 2013-03-20T10:03:56.063 回答
0

get_class() 函数应该可以工作:

http://php.net/manual/en/function.get-class.php

我同意 GRoNGoR 的观点,即您不需要获取实例化对象的属性的父类。相反,您可以在访问属性之前获取类的名称。例如:

$class = new MyClass();
$parent_class = get_class($class); // returns "MyClass"
$arrayReference = &$class->array;

当您拥有对象实例并且可以轻松地从那里获取父类时,不确定为什么您需要属性的父类。

于 2013-02-19T10:14:35.603 回答