我想要在 PHP 类中定义一些构造函数。但是,我的构造函数代码目前非常相似。如果可能的话,我宁愿不重复代码。有没有办法从 php 类的一个构造函数中调用其他构造函数?有没有办法在 PHP 类中有多个构造函数?
function __construct($service, $action)
{
if(empty($service) || empty($action))
{
throw new Exception("Both service and action must have a value");
}
$this->$mService = $service;
$this->$mAction = $action;
$this->$mHasSecurity = false;
}
function __construct($service, $action, $security)
{
__construct($service, $action); // This is what I want to be able to do, so I don't have to repeat code
if(!empty($security))
{
$this->$mHasSecurity = true;
$this->$mSecurity = $security;
}
}
我知道我可以通过创建一些 Init 方法来解决这个问题。但是有没有办法解决这个问题?