


Introduction to the differences and usage between new static() and new self()
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);
This is crazy, I have only seen
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. 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
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" */
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!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
