The static PHP member and instance member seem different. Why does PHP do this (without warning)?

<?php class A { public function instanceFunc() { echo "instance"; } public static function staticFunc() { echo "static"; } } A::instanceFunc(); // instance $a = new A(); $a->staticFunc(); // static 

This means that there is no difference between the static PHP method and the instance method. Zend doesn't even complain (WITHOUT WARNING).

In the Zend Engine. both the static method and the instance method, all stored in zend_class_entry.function_table.

Why does PHP behave this way? Is this a bug or a function?

+4
source share
3 answers

This is a potential duplicate. Does any static method in PHP have a non-stationary method? .

You will receive an error message if the E_STRICT warnings are enabled, otherwise you can silently call a non-static method, as if it were static. As mentioned in the answers here and on another question, the static / non-static method of the real world will not do without access to $this or self , as it was written to expect.

Refer to the docs: http://www.php.net/manual/en/language.oop5.static.php

Here's something to play with:

http://codepad.org/Gbu9T6Zv

+3
source

If you try to run the code below and look at the output of backtrace, you will see that PHP will convert instanceFunc () to a static method when called in a static context. However, in the context of the instance, it will consider it as a call to the instance.

If you introduce instance variables into the mix (delete the commented lines), then a fatal error occurs when calling instanceFunc () from a static call.

This means that PHP allows you to use all static methods (without any instance variables) to call from a static context as soon as this contract is broken, but an error will be generated. Therefore, the use of static functions seems to be only good practice in accordance with other object-oriented languages.

Regarding staticFunc (), both calls show that PHP interprets them as static calls, which is to be expected.

 class A { private $x = 5; private $y = 6; private $z; public function instanceFunc() { //$this->z = $this->y * $this->x; //echo $this->z; var_dump(reset(debug_backtrace())); } public static function staticFunc() { var_dump(reset(debug_backtrace())); } } $a = new A(); A::instanceFunc(); // static call of intended instance method $a->instanceFunc(); // instance call of instance method A::staticFunc(); $a->staticFunc(); 

Sample output (code works with comments):

 array(6) { ["file"]=> string(59) "test.php" ["line"]=> int(19) ["function"]=> string(12) "instanceFunc" ["class"]=> string(1) "A" ["type"]=> string(2) "::" ["args"]=> array(0) { } } array(7) { ["file"]=> string(59) "test.php" ["line"]=> int(22) ["function"]=> string(12) "instanceFunc" ["class"]=> string(1) "A" ["object"]=> object(A)#8 (3) { ["x:private"]=> int(5) ["y:private"]=> int(6) ["z:private"]=> NULL } ["type"]=> string(2) "->" ["args"]=> array(0) { } } array(6) { ["file"]=> string(59) "test.php" ["line"]=> int(24) ["function"]=> string(10) "staticFunc" ["class"]=> string(1) "A" ["type"]=> string(2) "::" ["args"]=> array(0) { } } array(6) { ["file"]=> string(59) "test.php" ["line"]=> int(26) ["function"]=> string(10) "staticFunc" ["class"]=> string(1) "A" ["type"]=> string(2) "::" ["args"]=> array(0) { } } 
+2
source

I'm not sure why he would not throw a warning, or at least an error. However, there are some significant differences in statics and instance. The static method cannot use class variables that are not static. This is easy to verify by adding a public / private variable that is non-stationary and tries to map it to staticFunc , which will staticFunc error.

I think that the main goal is to understand the differences between them and how to use them correctly. As to why PHP, at least, does not raise Notification, I don’t know, perhaps this is due to its laid-back nature. I am interested in the answer, who has more knowledge that he can give, this is just a little additional information.

I tested it with a full error message, and, of course, it really doesn't throw any notifications or warnings.


UPDATE

By doing some testing, it seems that when you call a non-stationary static function as a static function, you still cannot use private / public variables. Which, for ordinary functions, is likely to disable them. Which may be why the error or notification was never thrown. However, it would be nice if he was throwing something, since using an unmarked static function in this way is certainly not very good.

+1
source

All Articles