PHP构造函数参数?
我正在构建一个需要两个参数的类,它们可以通过PHP构造函数参数?,php,Php,我正在构建一个需要两个参数的类,它们可以通过\uu构造函数传递,也可以使用setter方法设置 检查参数是否通过构造函数传递的最佳方法是什么 我是这样做的: class Services { public function __construct(array $configs, array $services) { if(isset($configs) AND isset($services)) { $this->configs =
\uu构造函数
传递,也可以使用setter方法设置
检查参数是否通过构造函数传递的最佳方法是什么
我是这样做的:
class Services {
public function __construct(array $configs, array $services)
{
if(isset($configs) AND isset($services)) {
$this->configs = $configs;
$this->services = $services;
}
}
public function setConfigs(array $configs)
{
$this->config = $configs;
}
public function setServices(array $services)
{
$this->services = $services;
}
}
public function __construct(array $configs = false, array $services = false)
{
if($configs && $services) {
$this->configs = $configs;
$this->services = $services;
}
}
现在这很好,但如果这是正确的方法,我不是100%。
困扰我的是,如果参数通过构造函数传递,我希望两个参数都在那里,而不是只有一个
如何防止用户在构造函数中只放入一个参数?您可以添加php
is\u null()
函数,因此如果!为空(arg1)&&!is_null(arg2)
,那么您就没事了。您可以添加phpis_null()
函数,因此如果!为空(arg1)&&!如果为null(arg2)
,那么您就没事了。分别处理您传递的参数
public function __construct(array $configs, array $services)
{
if(isset($configs)) {
$this->configs = $configs;
}
if(isset($services)) {
$this->services = $services;
}
}
分别处理您传递的参数
public function __construct(array $configs, array $services)
{
if(isset($configs)) {
$this->configs = $configs;
}
if(isset($services)) {
$this->services = $services;
}
}
目前,您必须传递两个参数。要使它们成为可选的,您需要指定默认值。然后,您可以通过一个简单的检查来强制执行这两项:
public function __construct(array $configs = null, array $services = null) {
if ($configs === null xor $services === null) {
throw new InvalidArgumentException('Supply both or none!');
}
if ($configs && $services) {
$this->setConfigs($configs);
$this->setServices($services);
}
}
您不应该使用isset
,因为变量始终存在,因为它是函数签名的一部分。您只需要检查值。当前您必须传递两个参数。要使它们成为可选的,您需要指定默认值。然后,您可以通过一个简单的检查来强制执行这两项:
public function __construct(array $configs = null, array $services = null) {
if ($configs === null xor $services === null) {
throw new InvalidArgumentException('Supply both or none!');
}
if ($configs && $services) {
$this->setConfigs($configs);
$this->setServices($services);
}
}
您不应该使用
isset
,因为变量始终存在,因为它是函数签名的一部分。您只需要检查值。我会这样做:
class Services {
public function __construct(array $configs, array $services)
{
if(isset($configs) AND isset($services)) {
$this->configs = $configs;
$this->services = $services;
}
}
public function setConfigs(array $configs)
{
$this->config = $configs;
}
public function setServices(array $services)
{
$this->services = $services;
}
}
public function __construct(array $configs = false, array $services = false)
{
if($configs && $services) {
$this->configs = $configs;
$this->services = $services;
}
}
我会这样做:
class Services {
public function __construct(array $configs, array $services)
{
if(isset($configs) AND isset($services)) {
$this->configs = $configs;
$this->services = $services;
}
}
public function setConfigs(array $configs)
{
$this->config = $configs;
}
public function setServices(array $services)
{
$this->services = $services;
}
}
public function __construct(array $configs = false, array $services = false)
{
if($configs && $services) {
$this->configs = $configs;
$this->services = $services;
}
}
你测试过了吗?当您在函数头
\u构造(array$configs,array$services)
中定义了类型后,您可以确保一个用户必须通过两个数组
$test = new Services(array());
失败-可捕获的致命错误:传递给服务的参数2::\u construct()必须是数组
$test = new Services('','');
失败-可捕获的致命错误:传递给服务的参数1::\u construct()必须是数组
只有
允许 你测试过吗?当您在函数头
\u构造(array$configs,array$services)
中定义了类型后,您可以确保一个用户必须通过两个数组
$test = new Services(array());
失败-可捕获的致命错误:传递给服务的参数2::\u construct()必须是数组
$test = new Services('','');
失败-可捕获的致命错误:传递给服务的参数1::\u construct()必须是数组
只有
允许 我宁愿不这样做,最好不要混合类型,你应该使用
array$configs=array()
不允许设置任何值。我宁愿不这样做,最好不要混合类型,你应该使用array$configs=array()
不允许设置任何值。怎么样!(is_null(arg1)| is_null(arg2))
?它得到相同的结果,并且代码长度相同。所以这两种方法都是正确的:)那么呢!(is_null(arg1)| is_null(arg2))
?它得到相同的结果,并且代码长度相同。所以这两种方法都是正确的:)