php5.3 after static binding usage detailed _php skill

Source: Internet
Author: User
Tags echo b

This example describes the static binding usage after php5.3. Share to everyone for your reference, specific as follows:

Original manual:

Since PHP 5.3.0, PHP has added a feature called late static binding, which is used to reference statically invoked classes within the inheritance scope.

To be exact, late-stage static binding works by storing the class name of the last non-forwarding call. When a static method call is made, the class name is the one that is explicitly specified (usually on the left side of the:: operator), and the class to which the object belongs when a Non-static method call is made. The so-called "forward invocation" (forwarding call) refers to static calls made in the following ways: self::,parent::,static:: and Forward_static_call (). The Get_called_class () function can be used to get the class name of the method being invoked, static:: The scope is pointed out .

This feature is considered to be named "Late-static binding" from the language's internal perspective. "Late binding" means static:: is no longer resolved to define the class in which the current method resides, but is computed at the actual runtime. It can also be called a "static binding" because it can be used (but not limited to) a call to a static method.

Self:: Limitations of

Use self:: or __class__ a static reference to the current class, depending on the class in which the current method is defined:

Example #1 Self:: Usage

<?php
class A {public
static function, who () {
echo __class__;
}
public static function test () {
self::who ();
}
}
Class B extends A {public
static function, who () {
echo __class__;
}
}
B::test ();
? > 

The above routines will output:

A

Late static binding usage later statically bound the limit is bypassed by introducing a new keyword to represent the class that was originally invoked at runtime. Simply put, this keyword allows you to invoke test () in the example above to refer to the class B instead of A. The final decision is not to introduce new keywords, but to use the static keyword that has been reserved.

Example #2 static:: Simple usage

<?php
class A {public
static function, who () {
echo __class__;
}
public static function test () {
static::who ();//late static binding starting from here
}
class B extends A {public
STA Tic function who () {
echo __class__;
}
}
B::test ();
? > 

The above routines will output:

B

Note: In a non-static environment, the class that is invoked is the class to which the object instance belongs. Because $this-> tries to invoke a private method in the same scope, static:: It may give a different result. Another difference is static:: Only for static properties.

Example uses static #3 non-static environments:

<?php
class A {
private function foo () {
echo "success!\n";
}
The Public Function test () {
$this->foo ();
Static::foo ();
}
Class B extends a {*
* * FOO () is copied to B, hence's its scope would still be
successful * /
}
class C extends A {
private function foo () {/
* original method is replaced; the scope of the new One is C *
/}}
$b = new B ();
$b->test ();
$c = new C ();
$c->test (); Fails
?> 

The above routines will output:

success!
success!
success!
Fatal Error:call to Private Method C::foo () from the context ' A ' in/tmp/test.php in line 9

Note: The parsing of the late static binding continues until a fully parsed static call is made. On the other hand, if the static call uses parent:: or self:: The invocation information is forwarded.

Example #4 forwarding and non-forwarding calls

<?php
class A {public
static function foo () {
static::who ()
}
public static function The WHO () {
echo __class__. \ n ';
}
}
Class B extends A {public
static function test () {
a::foo ();
Parent::foo ();
Self::foo ();
}
public static function The WHO () {
echo __class__. \ n ';
}
}
Class C extends B {public
static function, who () {
echo __class__. \ n ';
}
}
C::test ();
? > 

The above routines will output:

A
C
C

The following example analyzes a class that references a static invocation within an inheritance scope based on the late static binding capability of PHP.

First look at the following code:

Class person
{public
static function status ()
{
self::getstatus ()
}
protected static function GetStatus ()
{
echo ' person is alive ';
}
}
Class Deceased extends person
{
protected static function GetStatus ()
{
echo ' person is deceased '; c43/>}
}
deceased::status ();//person is alive

Obviously, the result is not what we expected, because self:: Depends on the class in which it is defined, not the class in which it is running. To solve this problem, you might rewrite the status () method in an inherited class, and a better solution would be to add later static binding functionality after PHP 5.3.

The code is as follows:

Class person
{public
static function status ()
{
static::getstatus ()
}
protected static function GetStatus ()
{
echo ' person is alive ';
}
}
Class Deceased extends person
{
protected static function GetStatus ()
{
echo ' person is deceased '; c16/>}
}
deceased::status ();//person is deceased

Visible, Static:: Does not point to the current class, in fact, it is computed in run, forcing to get all the properties of the final class.

Therefore, it is recommended that you do not use self again::, using static::

Add:

User Post 1

PHP late static binding, how to explain? The following image output is A,c,c

The inheritance relationship of graphs shows that C completely contains B and a.

Before looking at the results of the answer, he observed that three classes have the same name who () method.
The system will use the last highest priority, and further, you can hardly go through C to call the WHO () within a or B, only to change the method, such as adding a getbwho () {echo b::who ();}
And then through C::getbwho (), to call the WHO () within B;

Here's how the results work:

Test only appears in B, so the result must be three results running in test ():

The first: static directly named to the surname of the call a static function, which is not suspense, must be a
Second: Parent:: Is the call to the previous level of the class, in this question for a,a and directly call static:who (); Above mentioned, this who () the highest priority in C, no matter where in your ABC call, as long as the static::who () It must be the last defined, overriding effect, if you want to call the required specified a::who () in a, or by removing the static from the scope limit. So the WHO () is defined in C
The third: Self::who and the second similar problem, look like to go B, pay attention to coverage effect, to call the WHO within B must be b::who (), because the more advanced C has rewritten this method, if C does not have who, must be B, and so on. Therefore, it is necessary to call the WHO in C;

So the answer is: ACC

The code is as follows:

<?php
class A {public
  static function foo () {
    static::who ()
  }
  public static function The WHO () {
    echo __class__. \ n ';
  }
}
Class B extends A {public
  static function test () {
    a::foo ();
    Parent::foo ();
    Self::foo ();
  }
  public static function The WHO () {
    echo __class__. \ n ';
  }
}
Class C extends B {
  //public static function who () {
  //  echo __class__. \ n ";
  }
c::test ();
? >

Output is: A b b

User Post 2

(or for the code in the diagram above)

It's not a very clear handbook, is it?

"Late binding" means static:: is no longer resolved to define the class in which the current method resides, but is computed at the actual runtime. It can also be called a "static binding" because it can be used (but not limited to) a call to a static method.

#1说的有个小问题

"Self::foo (); This self is actually a class C. Do you understand? C::test () c inherits the test () method of B.

Inaccurate, self or class B, but the Foo method is not overridden by itself, so call the Foo method of parent Class A.

If self is actually a class C, then try Self::foo (), Change to self::who (), print C, but print B, which is the difference between self and static.

<?php
class A {public
static function foo () {
static::who ()
}
public static function The WHO () {
echo __class__. \ n ';
}
}
Class B extends A {public
static function test () {
a::foo ();
Parent::foo ();
Self::who ();
}
public static function The WHO () {
echo __class__. \ n ';
}
}
Class C extends B {public
static function, who () {
echo __class__. \ n ';
}
}
C::test ();
? >

Output is: A C B

User Post 3

A::foo (); A refers to class A, accessing the Foo method of Class A and the Who method
Parent::foo (),//calling the parent class of Class B--a the Foo method, and telling the Foo method that the original caller is C
Self::foo (); Self refers to the class that defines the method, that is B, but B does not define the Foo method, which passes the original caller C up,
///The parent's Foo method, and finally the Who method of C;

So this answers the upstairs question: if the Self::foo (); Change to Self::who (), because self refers to B, and B has the Who method, so the result is a B

Static calls Use Parent:: or Self:: The original invocation information is forwarded.

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.