PHP delay binding and non-lazy binding parsing

Source: Internet
Author: User
PHP Version: php5.6

Delay bindings are: Get_class ($this), Get_called_class (), New Static (), Static::
Non-deferred bindings are: Get_class (), __class__, new self (), self::

When using new static ()

<?phpclass newstatic{//instance of the class private $newStatic;            Determines whether $newstatic is empty, is empty, instantiates itself and is stored in $newstatic public function init () {if ($this-newstatic) {            $this-newstatic = new static ();        echo "This class has been initialized";        } else {$this-newstatic, exec ();    }}//The execution method of the class public function exec () {echo "The class newstatic has been executed";    }}class Sub extends newstatic{//overrides the Exec method in the parent class to distinguish the public function exec () {echo "The class Sub has been executed"; }}echo "

Replace Static with self

<?phpclass newstatic{//instance of the class private $newStatic;            Determines whether $newstatic is empty, is empty, instantiates itself and is stored in $newstatic public function init () {if ($this-newstatic) {  $this-newstatic = new self ();        Modify the static here to the self echo "the class has been initialized";        } else {$this-newstatic, exec ();    }}//The execution method of the class public function exec () {echo "The class newstatic has been executed";    }}class Sub extends newstatic{//overrides the Exec method in the parent class to distinguish the public function exec () {echo "The class Sub has been executed"; }}echo "

Conclusion:

    If you call a method in a subclass that contains new static () in the parent class, it instantiates the subclass, but if you are using the new self () in the parent class, then the parent class is instantiated, and new self () always points to the defined class, and new Static () The class at which the call is bound (delayed binding) This is the difference between new static () and new self (). This difference is actually the same as the static:: And Self:: The difference is the same, use static:: Call static method when calling a statically bound class, and self:: A method to the static class of the definition the additional point of the new self () is equivalent to the following: $ class = Get_class (); Note: There is no pass parameter in Get_class () $obj = new $class (), or $class = __class__; $obj = new $class (); new static () is equivalent to the following: $class = get_ Called_class (); $obj = new $class (); or $class = Get_class ($this); Here Get_class () has no transfer parameter effect is not the same, with the function itself of the characteristics of $obj = new $class ();

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.