Call non static method like static in Php Leave a comment

Lets make an example to see how we can call non static php methods like static method. But not forgot it’s not a static method.

(new ClassName())->methodName();

Example:

echo 'Start..';

class testClass {
    public function testFunction() {
        return 'Test Value';
    }
}


 $testVar = new testClass();
 $testOutput = $testVar->testFunction();

// Output 1
 echo $testOutput;


// Output 2
 echo (new testClass())->testFunction();

Output 1 and Output 2 given same result.

Leave a Reply

Your email address will not be published. Required fields are marked *