Share nine: PHP easy to confuse syntax

Source: Internet
Author: User

One: late PHP static binding

Starting with php5.3, PHP adds a feature called late binding that references statically invoked classes within the scope of the inheritance

This feature is named "Late static binding" from the perspective of the language, and "late binding" means: Static:: It is no longer parsed to define the class in which the current method is located, but is calculated at the actual run time, and can also be a "static binding", because he can be used (but not limited to static method invocation).

Self:: the limit

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

Example:

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 (); A

Print Result: A

Use of late static bindings

Late static binding attempts to bypass the restriction by introducing a keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to refer to the Class B instead of a When you call Test () in the above. The final decision does not refer to the new keyword, but instead uses the reserved static keyword

Example:

<?phpclass A {public static function who () {echo __class__; The public static function test () {static::who ();///Late static binding starts here}}} class B extends A {public static funct    Ion who () {echo __class__; }} b::test ();? >

The printing result is: B

Late static binding is handled in a way that solves static calls that have not been resolved in the past, and on the other hand, if a static call uses the parent:: or self:: forwards the call information

<?phpclass A {    public static function foo () {         static::who ();    }      public static function who () {        echo __class__. \ n ";     }} class B extends A {    public static function test () { & Nbsp;      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 ();? > 

Printing results:

A c C

Example:

Printing results:
A

B

Special statement:

Get_called_class (); Gets the name of the class called in a static method

<?php
Class 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 ();
?>

Share nine: PHP easy to confuse syntax

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.