Common Concepts of PHP Confusion (vii) The difference between self, static, and parent

Source: Internet
Author: User
Tags php class

Objective

First of all, this static symbol static(静态)关键字 is not a thing. There are two ways to use these three symbols in a PHP object:

    1. Within a class, you can new self use new static , new parent create new objects
    2. You can self:: use static:: , parent:: call static variables, and methods.
Create a new object
 <?php class test{    public static function test_self(){        return new self();    }    public static function test_static(){        return new static();    }    public static function test_parent(){        return new parent();    }}class test2 extends test{    public static function test_parent(){        return new parent();    }}class test3 extends test2{}echo get_class(test::test_self());  //testecho get_class(test::test_static()); //testecho get_class(test2::test_self());  //testecho get_class(test2::test_static()); //test2echo get_class(test2::test_parent()); //testecho get_class(test3::test_self()); //testecho get_class(test3::test_static()); //test3 echo get_class(test3::test_parent()); //test

From the above example, it can be concluded that:

    1. new selfThe object created is the new self object created by the defined class
    2. new staticObject created is an new static object created by the executing class
    3. new parentThe object created is the new parent object created by the defined parent class (PHP5.3 introduced)
Calling static variable Concepts
    1. Forward invocation (forwarding call): The so-called "forward call" refers to static calls made in the following ways: self:: , parent:: static:: as well forward_static_call() . That is, calls that do not name a class when making a static call belong to a forward call.
    2. Non-forwarding dispatch (Non-forwarding call): A non-forwarding invocation is actually a static call (Foo::bar ()) and a non-static call ($foo->bar ()) that explicitly specifies the class name. That is, the static and non-static calls to the class name are explicitly specified.
    3. Late static binding (late static Bindings): "Late static binding" means, Static:: It is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time.
When there is no inheritance.

Self and static are no different.

  • In a static function, self and static can call static properties and methods (without instantiating the class so that non-static properties and methods cannot be called).
  • In non-static functions, self and static can call non-static properties and non-static methods.

     <?php class demo{public static $static;            Public $Nostatic;                   Public Function __construct () {self:: $static = "static";      $this->nostatic = "nostatic";      } public static function get () {return __class__;      Public function Show () {return ' This was function show with '. $this->nostatic; } Public Function Test () {echo Demo:: $static. "  \ n "; Use the type to call the static property static Echo Demo::get ().  \ n "; Use the class name to invoke the non-static method Demo echo Demo::show (). "  \ n "; Call the static method with the class name this is function show with nostatic echo Self:: $static. "  \ n "; Self calls the static property static Echo Self::get ().  \ n "; Self calls the non-static method Demo echo Self::show (). "   \ n "; The self calls the static method that is the function show with nostatic echo static:: $static. " \ n ";//static call static property static Echo Static::get ()." \ n ";//static call non-static method Demo echo Static::show ()." \ n "; Static method This is calledis function show with nostatic}} $obj = new Demo (); $obj->test ();
When there is an inheritance relationship
  • Methods and properties called by self always represent methods and properties of the current class
  • The methods and properties of the static call are methods and properties of the currently executing class
  • The methods and properties that are called by the parent are methods and properties of the parents class

      <?php class a{static $test = "AAA";      static function GetClassName () {return "A";      } function GetClassName2 () {return ' AA ';          } static function Testself () {echo self::getclassname ();          echo "\ n";            Echo Self::getclassname2 ();          echo "\ n";      echo self:: $test;          } static function Teststatic () {echo static::getclassname ();          echo "\ n";          Echo Static::getclassname2 ();          echo "\ n";      echo Static:: $test;       }} class B extends a{static $test = "BBB";        static function GetClassName () {return "B";        } function GetClassName2 () {return ' BB ';            } static function Testparent () {echo parent::getclassname ();            echo "\ n";            Echo Parent::getclassname2 ();            echo "\ n";      Echo Parent:: $test;        }} class C extends b{static $test = "CCC"; Static FunctiOn GetClassName () {return "C";        } function GetClassName2 () {return ' CC ';             } static function Testparent () {echo parent::getclassname ();             echo "\ n";             Echo Parent::getclassname2 ();             echo "\ n";        Echo Parent:: $test; }} b::testself ();  A AA AAA echo "\ n";  B::teststatic ();//B BB BBB echo "\ n";  B::testparent ();//A AA AAA echo "\ n"; C::testself ();  A AA AAA echo "\ n"; C::teststatic ();  C CC CCC echo "\ n"; C::testparent (); B BB BBB
Explain the examples in the handbook.
<?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(); ?>

The final output is a C c
Take out the analysis separately

public static function test() {            A::foo();            parent::foo();            self::foo();     }     
    1. A::foo(): Non-forwarding request, call a foo() method directly, call the result in any place is the same
    2. parent::foo(): in class B, the method of invoking the parent class A of B foo() (the use of the parent), foo() and the class name of the static::who() previous non-forwarding request ( written in the method of Class A) foo() , get_called_class() in the Class A, is called C, This is the currently executing class ), so call the C class who() method ( This step can be understood as a late static binding, that is, the use of static); The C class overrides the who() method, so the result is C; If you remove the Method, the method in class B is called, and who() who() if the method in class B is removed who() , the method in Class A is called who() .
    3. self::foo(): Executes the method in Class B foo() (the use of self), there is no method in Class B, and foo() inherits the method of Class A, foo() if a method is defined in Class B foo() , the method in class B foo() is executed; foo()method, as above.

Common Concepts of PHP Confusion (vii) The difference between self, static, and parent

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.