Home > Backend Development > PHP Tutorial > Introduction to the difference between new static() and new self() in PHP, staticself_PHP tutorial

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

WBOY
Release: 2016-07-13 10:09:57
Original
909 people have browsed it

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

The night is long!

Today I led the team to build a local website. I found that I couldn’t build it with PHP 5.2. The PHP code of the website contained many parts that were above 5.3. My boss asked me to change it so that it could run under 5.2.

I changed and found a place

Copy code The code is as follows:

return new static($val);

This damn horse is amazing, I’ve only seen it before
Copy code The code is as follows:

return new self($val);

So I checked online to find out the difference between the two.

self – This is this class, this class in the code segment.

static – PHP 5.3 only adds the current class, which is a bit like $this. It is extracted from the heap memory and accesses the currently instantiated class, so static represents that class.

Let’s take a look at the professional explanations from foreigners.

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 you call 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 code The code is 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 easy to understand at a glance.

I understand the principle, but the problem has not been solved yet. How to solve the problem of return new static($val);?

In fact, it is simple to use get_class($this); as follows

Copy code The code is 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"
*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/940493.htmlTechArticleIntroduction to the difference between new static() and new self() in PHP, staticself is a long night! Today, I led the team to build a local website. I found that it cannot be built using PHP 5.2. There are many PHP codes in the website...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template