0
votes

Class structure

In a class function i need to know if the current class is called by another class. Debugging with netbeans/xdebug i can see this class instance has a property called *yii\base\Model*__instances (the current class extends yii\base\Model).

*yii\base\Model*__instances is an array and inside there's an instance of the different classes. How can i get the key or classname of the first element of this array? (in this case app\models\User) Can i access __instances at all?

1
What is the real problem you face? Why do you want to determine which class calls your method? - Nico Haase
If the method is called from a certain class (app\models\User) i need to call another function. I already solved this with a workaround, but a cleaner solution would be better. - dgtal
If you want a clean solution, drop the idea completely - this is really ugly context-based magic. If method should behave differently depending on context, pass context as method argument. - rob006

1 Answers

-1
votes

As above. What is the actual problem you face? To get class in php you can find built-in get_class http://php.net/manual/en/function.get-class.php . To access the first element of sequential (indexed) array it would be $array[0] , in php 7.3 an above you could use http://php.net/manual/en/function.array-key-first.php , on associative array in previous versions of php you could simply iterate over and get the first one.