Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP difference in accessing class methods

Tags:

methods

php

class

What's the difference between $foo->bar() and $foo::bar()?

like image 364
TiGi Avatar asked Feb 18 '26 13:02

TiGi


1 Answers

$foo::bar() is a call of the static method bar(), that means the object $foo was not instanciated by the __construct() method.

When calling $foo->bar(), the object $foo has to be instanciated before! Example:

$foo = new Foo; // internally the method __constuct() is called in the Foo class!
echo $foo->bar(); 

Often you don't call a static method on a existing object like in you example ($foo), you can call it directly on the class Foo:

 Foo::bar();
like image 113
powtac Avatar answered Feb 20 '26 02:02

powtac



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!