class Test {
public function __construct() {
self::runTest();
Test::runTest();
}
public static function runTest() {
echo "Test running";
}
}
// echoes 2x "Test running"
new Test();
Is there any difference between self::runTest() and Test::runTest()? And if so, which one should I use?
self::runTest() when calling the method within the class and Test::runTest() when outside the class?
Here’s a bit of example code to show what’s happening:
The take-away point is that
A::tellclass()always calls thetellclassmethod defined inA. Butself::tellclass()allows child classes to use their own version oftellclass()if they have one. As @One Trick Pony notes, you should also check out late static binding: https://www.php.net/manual/en/language.oop5.late-static-bindings.php