我还添加了以下内容
declare(strict_types = 1);
简短代码测试严格类型
Class UsingStrict {
private $name;
function __construct(string $name) {
try {
$this->name = $name;
} catch (TypeError $e) {
echo "Caught the exception";
}
}
}
$variable = new UsingStrict(0);
哥登
Fatal error: Uncaught TypeError: Argument 1 passed to UsingStrict::__construct() must be of the type string, int given
期望
Caught the exception
我对此很陌生,难道无法在构造函数中捕获异常吗?