0

Guess it's impossible design for Hacklang?

<?hh //strict

 abstract class Foo {

    public static function bar():void {

        $class = get_called_class();
        $instance = new $class();

        // do stuff

    }
}

Can't use new on classname 'Foo'; __construct arguments are not guaranteed to be consistent in child classes (Typing[4060])

4

1 回答 1

2

您需要用<<__ConsistentConstruct>>-- 注释您的类,因为默认情况下您可以更改子类中构造函数的签名,否则实例化将是不安全的,因为参数列表可能已更改。您可以从官方文档我写的这篇博客文章中阅读更多关于该功能的叙述

顺便说一句,您可以更换

$class = get_called_class();
$instance = new $class();

更好的

$instance = new static();
于 2016-02-18T23:18:12.297 回答