PHP Classes: get access to the calling instance from the called method
oop, php
Solution
You can pass a reference to the first object like this:
class normalClass {
protected $superObject;
public function __construct(superClass $obj) {
$this->superObject = $obj;
}
public function someMethod() {
//this method shall access the doSomething method from superClass
$this->superObject->doSomething();
}
}
class superClass {
public function __construct() {
//provide normalClass with a reference to ourself
$inst = new normalClass($this);
$inst->someMethod();
}
public function doSomething() {
//this method shall be be accessed by domeMethod form normalClass
}
}
Problem
sorry for that weird subject but I don't know how to express it in an other way. I'm trying to access a method from a calling class. Like in this example: ``` class normalClass { public function someMethod() { [...] //this method shall access the doSomething method from superClass } } class superClass { public function __construct() { $inst = new normalClass; $inst->someMethod(); } public function doSomething() { //this method shall be be accessed by domeMethod form normalClass } } ``` Both classes are not related by inheritance and I don't want to set the function to static. Is there any way to achieve that? Thanks for your help!