哪个更好foo :: bar()VS $ foo :: bar()?
问题描述:
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?
关于此: p>
class foo {
public static function bar(){
echo'hi';
}
}
code> pre>
这有什么区别: p>
foo :: bar();
code> pre>
以及: p>
$ obj = new foo ();
$ obj :: bar();
code> pre>
还是没有区别? 是正确的还是原则? 哪个更好? p>
div>
答
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.