+
95
-

PHP中new static()与new self()有啥区别?

php

PHP中new static()与new self()有啥区别?


网友回复

+
15
-

new static() 和 new self() 在 PHP 中具有相同的功能。

它们都是用来在类中创建新实例的。区别在于:- new static() 使用类本身,在 static 上下文中解析。- new self() 使用调用对象的类。示例:
<?php
class A {
    public static function doStuff() {
        return new static(); 
    }
}

class B extends A {
    
}

$a = A::doStuff();
echo get_class($a); // A

$b ...

点击查看剩余70%

我知道答案,我要回答