The difference between new self () and new Static () in PHP!

Source: Internet
Author: User
This article introduces the difference between New self () and new Static () in PHP, and shares it with everyone, as well as leaving a note for yourself.

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 S Tatic (); }} $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 ();p rint get_class ($sun 1->ge Tnewfather ());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.

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.