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