0

How can I figure out in what class a reference to a variable was initiated (and currently exists)?

Example:

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

My best bet is some kind of Reflection, but I haven't found any functions that seem suitable for this.

Edit:

A better suited example for what I want is the following:

<?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"
?>

Now, this works fine, but I'd love to be able to do this without using $parentReference in every single subvariable. It might not be possible, but I'd love to know whether it is or not.

6
  • 1
    What's $arrayReference = &$array; supposed to do? Typo? Commented Feb 19, 2013 at 10:06
  • @ÁlvaroG.Vicario Yep, it was a typo.
    – h2ooooooo
    Commented Feb 19, 2013 at 10:07
  • 1
    I'm not certainly sure if this is possible. However I think that you are trying to achieve something what looks like a bad practise/pattern. Could you explain why you need that? What it should be used for?
    – grongor
    Commented Feb 19, 2013 at 10:10
  • @GRoNGoR I'm writing a REST documentation for our framework and I'd love for it to be able to document itself by studying the class - this will not be used in runtime per-se, but rather just to create the documentation HTML files. PHPDoc is not suitable, as it also needs to figure out the REST commands itself.
    – h2ooooooo
    Commented Feb 19, 2013 at 10:11
  • i would assume that the array is generated in the memory somewhere else than the class. This would mean that the variable is not bound to the class. Just like in any other language (take java for example). You can eliminate the instance and still have a reference to the array.
    – galchen
    Commented Feb 19, 2013 at 10:16

2 Answers 2

1

For your example:

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

to find out to which variable originally the alias $arrayReference refers to is not possible in PHP. There is no function available resolving the aliases.

Additionally $class->array is just a variable on it's own. So you would also need to find out based on a value in which class it was defined. That is not possible as well, similar to that PHP does not offer anything to resolve a variable alias, it also does not offer anything to learn about the definition of a variable.

So in short PHP does not have a ReflectionVariable class available ;) I wonder if it is even possible.

2
  • From a technical standpoint (in any language), it should be possible as $class->array is linked to $class somewhere in the memory, and if $class->array would know to access the $array variable inside of the $class class, then the compiler should be able to backtrace memory as well.
    – h2ooooooo
    Commented Mar 20, 2013 at 10:32
  • 1
    Yes technically I can imagine this is possible. As well for the variable aliasing. Internally the ZVAL container keeps track of that as well otherwise it won't work. Maybe it's worth to create a feature request for such a ReflectionVariable? One should make up it's mind about the interface first though.
    – hakre
    Commented Mar 20, 2013 at 11:18
0

The get_class() function should work:

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

I agree with GRoNGoR that you shouldn't need to get the parent class of a property of an instantiated object. You could instead just get the name of the class before accessing the property. For example:

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

Not sure why you'd need the parent class of the property when you have the object instance and can easily get the parent class from there.

Not the answer you're looking for? Browse other questions tagged or ask your own question.