What is the calling scope?
In PHP, when calling a method, $ this pointer to the object is the calling scope moment method is called for the following example:
<?php Foo::bar(); ?>
When call bar method, in the context of a domain is not calling scope, so this is a static call.
For the following example:
<?php class A { public function test() { Foo::bar(); } } $a = new A(); $a->test();
When call bar method, in the context of a $ a object, that is to say, at this time the calling scope is $ a target, so this really is not a static call.
To verify this conclusion, a practical example consider the following:
<?php class Foo { public function bar() { var_dump($this); } } class A { public function test() { Foo::bar(); } } $a = new A(); $a->test(); ?>
What output do?
object(A)#1 (0) { }
When you call the bar, seemingly "static" call to call, $ this pointer is assigned, and points to the $ a object, that is fairly static call it?
I give this example to illustrate this problem, but we in practical applications, we try to avoid the use of "::" to invoke a non-static method, PHP will give a Strict warning for this call:
- Strict Standards: Non-static method Foo::bar() should not be called statically, assuming $this from incompatible context
Some might say that this should be considered a bug, right? In fact, more should be caused by incorrect use, because you have a calling scope in the context of a "static form of" call non-static method of a class period.
So why should such a design does PHP Consider the following example?:
<?php class A { public function __construct() { } } class B extends A { public function __construct() { parent::__construct(); } }
When we call the constructor of the parent class, we are interested in the current scope should be passed to the constructor of the parent class as the calling scope.
:: calling for the use of non-static methods of a class of this operation is wrong, we have to use the correct way to call non-static methods in a class for A :: get () This method, although able to call to Class A non-static method get (), but it will generate an error message
“Strict Standards: Non-static method Foo::bar() should not be called statically, assuming $this from incompatible context”
So, we should try to call non-static methods using the correct way.
Transfer from birds Gebo off: