Introduction to the differences and usage between new static() and new self()

伊谢尔伦
Release: 2023-03-11 08:04:01
Original
1407 people have browsed it

This article mainly introduces the analysis of the differences and similarities between new static() and new self() in PHP. It is a very practical skill. I believe it can bring benefits to everyone learning PHP programming. Some help.

The cause of the problem is building a site locally. I found that it cannot be built with PHP 5.2. There are many parts of the site PHP code that are above 5.3, and the changes are required to run under 5.2.

After changing and changing, I found a place

return new static($val);
Copy after login

This is crazy, I have only seen

return new self($val);
Copy after login

, 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. Then static represents that class. .

Let's take a look at the professional explanation 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() ).

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
Copy after login

This example is basically understandable 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 as simple as using get_class($this); 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"
*/
Copy after login

Interested friends can test the sample code and I believe there will be new gains!

The above is the detailed content of Introduction to the differences and usage between new static() and new self(). For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!