This article brings to you the content is about the PHP non-forwarding calls and forwarding calls code instances, there is a certain reference value, the need for friends can refer to, I hope to help you.
To statically access members of a class:
1. Non-forwarding calls
1) class name Explicit (A::funca ())
2. Forwarding Calls
1) Self:: 2) Parent:: 3) Static:: 4) Forward_static_call () 5) Get_called_class ()
Attention:
Late binding means that static:: It is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time. It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.
<?phpclass A {public static function foo () { static::who (); } public static function who () { echo __class__. \ n "; }} Class B extends A {public static function test () { a::foo (); Parent::foo (); Self::foo (); } public static function who () { echo __class__. \ n "; }} Class C extends B {public static function who () { echo __class__. \ n "; }} C::test ();? >
Operation Result:
Acc
Note:
In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results. Another difference is static:: Only for static properties.
Inherited property belongs to itself
The inheritance chain first accesses the current class, accesses it according to the reserved word attribute, and then accesses it according to the inheritance chain.
$this Access member (call context)
Self:: Access member (parse context)
<?php class a{ protected $value = "A value"; Public Function Printvalue () { echo $this->value; }} Class B extends a{ protected $value = "B value";} $app = new B (); $app->printvalue ();
Run Result: B value
<?php class a{ //The property must be defined as static to use self: to invoke the static protected $value = "A static value"; Public Function Printvalue () { echo self:: $value; }} class B extends a{ static protected $value = "B static val UE ";} $app = new B (); $app->printvalue ();
Run Result: A static value
<?phpclass a{ static protected $value = "A static value";} Class B extends a{public function Printvalue () { echo self:: $value; }} $app = new B (); $app->printvalue ();
Run Result: A static value