Calling a function by chaining after the constructor

During the construct call the object doesn’t actually exist yet, I need to extract the object’s name by comparing the $GLOBALS arrays during and after the construct call. To automate this process and perform it in one step, you need to make sure that the function is called in the chain immediately after the constructor is called. Is it possible to implement this in php?


Answer 1, authority 100%

Actually, I can offer a procedural approach.

class MyClass {
  public $a = 1;
  public function __construct($objectName) {
    $GLOBALS[$objectName] = &$this;
    }
  }
new MyClass('myVar');
var_dump($myVar);

But keep in mind that objects will only be available in the global. Those. in functions you have to do this (in that order):

function a() {
  new MyClass('lol');
  global $lol;
  var_dump($lol);
  }