Your source code is not a good example of object oriented programming at all.
You SHOULD always pass firstclass into a secondclass using for example constructor. Then you can call first class from inside the secondclass.
Object injection
class firstclass
{
function method1()
{
return "foo";
}
}
class secondclass
{
/**
* @var firstclass
*/
private $firstClass;
/**
* @param firstclass $firstClass
*/
public function __construct(firstclass $firstClass)
{
$this->firstClass = $firstClass;
}
function method2()
{
return $this->firstClass->method1();
}
}
print_r((new secondclass(new firstclass))->method2());
Callable injection
If you don't want use the whole instance of firstclass inject callable inside the secondclass and then call it.
Calling method instance
class firstclass
{
function method1()
{
return "foo";
}
}
class secondclass
{
/**
* @var callable
*/
private $method1;
/**
* @param callable $method1
*/
public function __construct(callable $method1)
{
$this->method1 = $method1;
}
function method2()
{
return call_user_func($this->method1);
}
}
print_r((new secondclass(array(new firstclass, 'method1')))->method2());
Calling static method
class firstclass
{
static function method1()
{
return "foo";
}
}
print_r((new secondclass('firstclass::method1'))->method2());
staticif you intend to use them statically.