Comparison _php instance of new Static () and new self () in PHP

Source: Internet
Author: User

Today, in coding, I found the new static (), and felt that the instantiation should not be new self ()? Query to know the difference between the two:

1 when there is a child-class integration, the performance of the two is not the same

2 PHP 5.2 and the following version does not support the new static () syntax

In the simplest sense, self is what class it is written in, and the actual invocation is this class. The so-called late static binding, the static represents the use of this class, is the static you write in the parent class,

This static is then directly/indirectly used by subclasses, and this static refers to this subclass, so the static and $this are similar, but static can be used for statically methods and properties.

The specific explanations are as follows:

Self-This is the class, which is the class within the code snippet.

Static-php 5.3 Added to the current class, a bit like the meaning of $this, extracted from the heap memory, access to the current instantiation of the class, then static represents that class.

Or look at the foreigner's professional explanation:

Self refers to the same class whose method the new operation takes place in.

Static in PHP 5.3 's late static bindings refers to whatever class in the hierarchy of the call the "method" on.

In the following example, B inherits both methods from a. Self are bound to a because it's defined in a ' s implementation of The the whereas static is bound to the called Class (also and the Get_called_class ()).

Code on:

Class Person {public
static function get_self () {return
new self ();
}
public static function Get_static () {return
new static ();
}
}
Class Wangbaoqiang extends person{}
echo Get_class (Wangbaoqiang::get_self ());//person
Echo Get_class ( Wangbaoqiang::get_static ()); Wangbaoqiang
Echo Get_class (Person::get_static ());//person

But if you want the subclass to use Get_class, the return is also the name of the current subclass (' Wangbaoqiang ').

<?php
class Person {public
function create1 () {
$class = Get_class ($this);
return new $class ();
}
Public Function Create2 () {return
new static ();
}
}
Class Wangbaoqiang extends person {
}
$wangBaoQiang = new Wangbaoqiang ();
Var_dump (Get_class ($wangBaoQiang->create1 ()), Get_class ($wangBaoQiang->create2 ()));
/* The result 
string (1) "Wangbaoqiang"
string (1) "Wangbaoqiang"
* *

The above is a small set of PHP to introduce you to the new static () and new self () comparison, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.