这是在php:中强制执行字符串构造函数参数的公认方法吗
public function __construct($myParameter)
{
if (! is_string($myParameter)) {
throw new InvalidArgumentException('My parameter must be a string');
}
$this->myParameter = $myParameter;
}
AsPHP(版本7之前)不允许typehint任何简单类型(int、string、boolean、float),而只允许数组、类和接口,这样的类型检查需要手动完成。PHP提供了一系列is_*函数,这些函数可以帮助检查变量的类型:http://php.net/manual/en/ref.var.php
回答您的问题,是的,您提供的是强制执行函数类型参数的一种相当常见的方式。