深入理解PHP中new self()和new static()的区别
这篇文章主要介绍了PHP中new self()和new static()的区别,有一定的参考价值,感兴趣的小伙伴可以看看,希望可以帮助到你!
1、new static()是在PHP5.3版本中引入的新特性。
2.无论是new static()还是new self(),都是new了一个新的对象。
3.这两个方法new出来的对象有什么区别呢,说白了就是new出来的到底是同一个类实例还是不同的类实例呢?
为了探究上面的问题,我们先上一段简单的代码:
class Father { public function getNewFather() { return new self(); } public function getNewCaller() { return new static(); } }$f = new Father();print get_class($f->getNewFather());print get_class($f->getNewCaller());
注意,上面的代码get_class()方法是用于获取实例所属的类名。
这里的结果是:无论调用getNewFather()还是调用getNewCaller()返回的都是Father这个类的实例。
打印的结果为:FatherFather
到这里,貌似new self()和new static()是没有区别的。我们接着往下走:
Sun1 Sun2 (-> (-> (-> (->getNewCaller());
看上面的代码,现在这个Father类有两个子类,由于Father类的getNewFather()和getNewCaller()是public的,所以子类继承了这两个方法。
打印的结果是:FatherSun1FatherSun2
我们发现,无论是Sun1还是Sun2,调用getNewFather()返回的对象都是类Father的实例,而getNewCaller()则返回的是调用者的实例。
即$sun1返回的是Sun1这个类的实例,$sun2返回的是Sun2这个类的实例。
现在好像有点明白new self()和new static()的区别了。
首先,他们的区别只有在继承中才能体现出来,如果没有任何继承,那么这两者是没有区别的。
然后,new self()返回的实例是万年不变的,无论谁去调用,都返回同一个类的实例,而new static()则是由调用者决定的。
上面的$sun1->getNewCaller()的调用者是$sun1对吧!$sun1是类Sun1的实例,所以返回的是Sun1这个类的实例,$sun2同样的道理就不赘述了。
好了,关于PHP中new self()和new static()的区别就暂时说这么多,希望对读者的理解有所帮助,如果有不对的地方欢迎拍砖扔蛋。
【相关教程推荐】
2. php从入门到精通
3. bootstrap教程

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
