Differences and similarities between New Static () and new self (), staticself_php tutorial

Source: Internet
Author: User

The difference between new static () and new self (), staticself


The Night is long!

Today the leader builds a local station. Found in PHP 5.2 built up, the station PHP code inside there are a lot of more than 5.3 parts, the leader let me change in the 5.2 can run.

Changed to find a place.

return New Static ($val);

This is a horse of God, I saw

return new self ($val);

So the internet looked down, the difference between them two.

Self-Is this class, which is the class within the code snippet.

Static-php 5.3 Added only 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 the class.

Let's take a look at the professional explanation of the foreigner.

selfRefers to the same class whose method, the new operation takes place in.

staticIn PHP 5.3 's late static bindings refers to whatever class in the hierarchy which the method on.

In the following example, B inherits both methods from A . self was bound to a because it's defined in a ' s implementation of the first method, Whereas static is bound to the called Class (also see get_called_class () ).

class A {    publicstaticfunction  get_self () {        returnNew self ();    }      Public Static function get_static () {        returnnewstatic();    }} class extends A {} Echo Get_class (B::get_self ());  // A Echo Get_class // B Echo Get_class // A

This example is basically understood at a glance.

The principle is understood, but the problem has not been solved, how to resolve the return new static ($val); What about the problem?

In fact, it's simple.get_class($this); 如下

class A {    publicfunction  create1 () {        $classget_class(  $this);
return New $class (); } Public function Create2 () { returnnewstatic(); }} class extends A {} $b New B (); Var_dump (get_class($bget_class($b,create2 ())); /* The result string (1) "B" string (1) "B" */


What is the difference between C + + new and new[]?

Things are a little bit more. Oh. Give you a detailed talk.
1. The difference between new and new[]
New is used for the creation of a single object or instance, which is called the constructor of the class.
New [] is used to create an array instance of an object or instance, and the address is contiguous. (Memory allocations may not be contiguous, but the list of addresses is contiguous.) )
2. Virtual function (This can not be said, only examples of children)
Class Person
{
Public:
Virtual say ();
}
Class Techer:public Person
{
Public:
protected override say ();
}
Class Student:public Person
{
Public:
protected override say ();
}
The third one didn't understand what it meant.
The front two do not know you can understand No. Contact me if you don't have any.

Static a A=new a () in Java What's the use of static? Specific points

In Java
public static void Main (String args[]) This sentence is a master method,
A Java program can have multiple methods, but the main method has only one,
A ststic-modified method is called a class method (main is also a class method).
A a=new A () is to create an instance object static is a cosmetic action

http://www.bkjia.com/PHPjc/867540.html www.bkjia.com true http://www.bkjia.com/PHPjc/867540.html techarticle the difference between new static () and new self () is staticself long night! Today the leader builds a local station. Found in PHP 5.2 is not built up, the station PHP code inside there are a lot of more than 5.3 ...

  • 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.