Introduction to the difference between new static () and new self () in PHP, staticself_php tutorial

Source: Internet
Author: User

Introduction to the difference between new static () and new self () in PHP, 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.
Copy the Code code as follows:
return new static ($val);

This is a horse of God, I saw
Copy the Code code as follows:
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.

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 which the method on.

In the following example, B inherits both methods from A. Self is 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 ()).
Copy the Code code as follows:
Class A {
public static function Get_self () {
return new self ();
}

public static function Get_static () {
return new static ();
}
}

Class B extends A {}

Echo Get_class (B::get_self ()); A
Echo Get_class (B::get_static ()); B
Echo Get_class (A::get_static ()); 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 is simple to use Get_class ($this); As follows
Copy the Code code as follows:
Class A {
Public Function create1 () {
$class = Get_class ($this);
return new $class ();
}
Public Function Create2 () {
return new static ();
}
}

Class B extends A {

}

$b = new B ();
Var_dump (Get_class ($b->create1 ()), Get_class ($b->create2 ()));

/*
The result
String (1) "B"
String (1) "B"
*/

http://www.bkjia.com/PHPjc/940493.html www.bkjia.com true http://www.bkjia.com/PHPjc/940493.html techarticle the difference between new static () and new self () in PHP is introduced, 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 many ...

  • Related Article

    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.