PHP Non-forwarding calls and code instances for forwarding calls

Source: Internet
Author: User
Tags php class
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

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.