Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling static method within class

Tags:

php

static

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?

like image 445
Chris Avatar asked Mar 10 '26 23:03

Chris


1 Answers

you should call self::runTest() from inside the class methods and Test::runTest() outside of the class methods

like image 91
Mustafa Shujaie Avatar answered Mar 13 '26 15:03

Mustafa Shujaie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!