1.new static () is a new feature introduced in the PHP5.3 release.
2. Either new static () or new self () is a new object.
3. What is the difference between the new objects of the two methods, or is the new one the same class instance or a different class instance?
To explore the above questions, let's start with a simple code:
Class Father {public function Getnewfather () { return to new self (); } Public Function Getnewcaller () { return new static (); }} $f = new Father ();p rint get_class ($f->getnewfather ());p rint Get_class ($f->getnewcaller ());
Note that the above code Get_class () method is used to get the class name to which the instance belongs.
The result here is that both getnewfather () or call Getnewcaller () return an instance of the Father class.
The result is printed as: Fatherfather
There is no difference between the seemingly new self () and the New Static (). We then went down:
Class Sun1 extends Father {}class Sun2 extends Father {}
$sun 1 = new Sun1 ();
$sun 2 = new Sun2 ();
Print Get_class ($sun 1->getnewfather ());p rint Get_class ($sun 1->getnewcaller ());p rint get_class ($sun 2-> Getnewfather ());p rint Get_class ($sun 2->getnewcaller ());
Look at the code above, now this Father class has two subclasses, because the Father class's Getnewfather () and Getnewcaller () are public, so the subclass inherits the two methods.
The result of this printing is: fathersun1fathersun2
We found that either SUN1 or SUN2, the object returned by the call Getnewfather () is an instance of the class father, and Getnewcaller () returns the instance of the caller.
That is, $SUN1 returns an instance of the class Sun1, $sun 2 Returns an instance of the class Sun2.
Now it seems a bit clear that the difference between new self () and new Static ().
First of all, their differences can only be reflected in the inheritance, if there is no inheritance, then there is no difference between the two.
Then, the instance returned by new self () is perpetual, and whoever calls it returns an instance of the same class, and new static () is determined by the caller.
The caller of $sun1->getnewcaller () above is $sun1, right! $sun 1 is an instance of class Sun1, so returning an instance of the Sun1 class, $sun 2 The same reason is not to repeat.
Well, the difference between new self () and new Static () in PHP is so much that I want to be helpful to the reader's understanding, and if there is something wrong, welcome to shoot bricks and throw eggs.
A study of the difference between new self () and new Static () in PHP