About this:
class foo {
public static function bar() {
echo 'hi';
}
}
What's difference between this:
foo::bar();
and this:
$obj = new foo();
$obj::bar();
Or not difference? Are both the right and the principles? Which is better?
I believe that there is no difference between them, but from my experience most often used form is Foo::bar()
.
You can find some examples here.
There is static method example with usage:
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>
After that you can find example with accessing properties:
print Foo::$my_static . "
";
$foo = new Foo();
print $foo::$my_static . "
";
It means that both ways are correct. It's up to you what to use.