php static methods question

2019-04-08 16:56发布

What is the difference between these two pieces of code?

class something {

   static function doit() {
      echo 'hello world';
   }
}

something::doit();

and the same but without the static keyword

class something {

   function doit() {
      echo 'hello world';
   }
}

something::doit();

They both work the same is it better to use the static keywords? Am i right in understanding that it doesn't instantiate the class if you use the static method?

标签: php oop
6条回答
放荡不羁爱自由
2楼-- · 2019-04-08 17:22

Your second example is wrong. Using a static method does not create an instance of the class. Your second example should look like this:

$x = new something();
$x->doit();
查看更多
贼婆χ
3楼-- · 2019-04-08 17:41

The difference is that static functions can be used without having to create an instance of the class.

Have a look at this great PHP OOP beginner tutorial here. It explains in more detail with an example under the Static Properties and Methods section.

查看更多
Luminary・发光体
4楼-- · 2019-04-08 17:41

Second bit shouldn't work as you should call it by

$something = new something();
$something->doit();

Static functions allows you to call a function within a class without consturcting it. So basically if you have a class to handle users, so a function that logs the user in should be a static function, as in the constructor of that class you will probably gather the user information and you cannot do so without logging him in.

查看更多
萌系小妹纸
5楼-- · 2019-04-08 17:42

In addition to the other valid answers, the reason for the 2nd example working is also due to a quirk in how PHP handles objects and calls (Besides PHP 4 compatibility). Calling a non-static declared method statically from within another instance will let you access class methods on other classes as if they were local. To understand, let's take an example:

class A {
    public function foo() {
        echo get_class($this) . "\n";
    }
}

class B {
    public function bar() {
        A::foo();
    }
}

$a = new a();
$a->foo(); // "A"
$b = new B();
$b->bar(); // "B"

Did you see what happened there? Because you called the A::foo() method from within another class's instance, PHP treated the call as if it was on the same instance. Note that there is no relationship between A and B other than the fact that B calls A. Within A->foo(), if we did $this instanceof A (or $this instanceof self), it would fail and return false! Quite unusual...

Now, I first thought it was a bug, but after reporting it, it's apparently as designed. It's even in the docs.

Note that this will not work with E_STRICT mode enabled. It also will not work if you declare a method as static.

查看更多
走好不送
6楼-- · 2019-04-08 17:44

The second example is technically incorrect - if you turn on E_STRICT error reporting you'll see that PHP is actually throwing an error.

PHP Strict Standards: Non-static method something::doit() should not be called statically in...

In other words, it's being nice and letting you call the function anyway.

查看更多
戒情不戒烟
7楼-- · 2019-04-08 17:46

Static methods should be declared static for minimum two reasons:
a) when using E_STRICT error_reporting, calling non static method as static will generate error:
Strict standards: Non-static method something::doit() should not be called statically
b) based on keyword static some IDE's filter method possible to run at auto-complete.

查看更多
登录 后发表回答