Wondering how the example below is actually working, and how one would be able to do something like dynamically. Using call_user_func or call_user_func_array doesn’t allow this to happen.
<?php
class Person
{
public $name = "George";
public function say_hi()
{
return ExtraMethods::hi();
}
}
class ExtraMethods
{
public function hi()
{
return "Hi, ".$this->name;
}
}
$george = new Person();
echo $george->say_hi();
?>
this should result with:
Hi, George
Wondering why the instance method hi can be called not only statically (not surprised that this can happen in PHP), but why I am able to use $this
From the manual:
So, according to the second portion, by design. Keep in mind it uses the actual object instance in existance though (in other words, if you add
public $name = "SomethingElse";toExtraMethods, the result would still beHi, George).Calling the method statically is not proper coding, but PHP forgives you, and issues only a Strict Error:
Of course, in this instance, just passing the object as an argument would be far clearer and preferable.