我知道PHP的一些优点来自于松散的类型,但我想创建一个具有严格类型、面向对象函数的PHP类。
我尝试了我认为有效的方法:
... [index.php]
24: include './documentation-builder.php';
25: $builder = new DocBuilder('Documentation');
... [documentation-builder.php]
5: class DocBuilder
6: {
7: function __construct(string $title)
8: {
9: $this->title = $title;
10: }
11: }
但我得到了以下错误:
可捕获的致命错误:传递给DocBuilder::__construct((的参数1必须是字符串的实例,给定字符串,在第25行的''path''to''index.php中调用,并在第5行的''path''to_documentation-builder.php中定义
事实上,"参数1…必须是字符串的实例,给定字符串"让我觉得我很接近,但忘记在某个地方设置标志。
如何要求在PHP函数上严格键入
目前只支持类和数组的类型提示。
类型提示不能与int或string等标量类型一起使用。资源和特性也不允许。
源