如果构造函数重载不可能,那么如何在PHP中创建具有不同参数的对象?
这是Employee.class.php如果构造函数重载不可能,那么如何在PHP中创建具有不同参数的对象?,php,oop,Php,Oop,这是Employee.class.php class Employee { public $first_name; public $last_name; public $date_of_birth; public function __construct($fn, $ln, $dob) { $this->first_name = $fn; $this->last_name = $ln; $this->date_of_b
class Employee
{
public $first_name;
public $last_name;
public $date_of_birth;
public function __construct($fn, $ln, $dob)
{
$this->first_name = $fn;
$this->last_name = $ln;
$this->date_of_birth = $dob;
}
public function registerEmployee()
{
require '../config.php';
$stmt = $dbh->prepare("INSERT INTO emp_reg(e_name,
e_lname,
e_dob) VALUES(?,?,?)");
$stmt->execute(array($this->first_name,
$this->last_name,
$this->date_of_birth));
echo "Saved Successfully";
}
public function return_employee_data($employee_id)
{
require '../config.php';
$stmt = $dbh->query("SELECT * FROM emp_reg WHERE e_id = '$employee_id'");
$arr = $stmt->fetchall(PDO::FETCH_ASSOC);
$res = json_encode($arr);
echo $res;
}
}
当我需要在其他文件中使用这个类时,比如说xyz.php
只是为了
return_employee_data($employee_id);
我必须在该文件中创建一个对象,如
// constructor overloading is not possible so I can't create `$EmployeeObject` like this.
$EmployeeObject = new Employee();
所以我不能运行这个函数返回员工数据($employee\u name)代码>像这样
$EmployeeObject->return_employee_data($employee_name); //not possible in this new file
如果构造函数重载不可能,那么如何创建具有给定参数且没有任何参数的对象?我还想在我们只包含类定义的其他文件中创建具有可变参数的对象,而文件数据要么不提供数据,要么提供变量数据来创建具有上述定义的对象
如果我们无法创建对象,我如何调用其底层函数来解决任何特定问题?我认为最好的方法是使用helpers方法,如:
$employee = Employee::withName($name);
如果您对这种方法感兴趣,请查看@Kris提供的完整答案。首先,您的答案中有不止一个问题
a)为了让您能够拥有多种形式的“构造函数”(但仍然有限),请参见以下示例
class Foo{
public $dmg;
public $speed;
public function __construct($dmg = '2', $speed = '3'){
$this->dmg = $dmg;
$this->speed = $speed;
}
}
$a = new Foo();
echo $a->speed."\n"; //3
$a = new Foo(6,6);
echo $a->speed."\n"; //6
b)您的函数return\u employee\u data
不依赖于类中的任何内部结构,这样,如果您想在外部使用它,可以将其设置为静态(在定义前面写入静态),以便将其用作employee::return\u employee\u data($id)
public function __construct($fn = null, $ln = null, $dob = null)
{
if($fn === null && $ln === null && $dob === null ) {
$this->constructNothing();
}
else {
$this->constructFull($fn,$ln,$dob);
}
}
private function constructFull( $fn, $ln, $dob ) {
$this->first_name = $fn;
$this->last_name = $ln;
$this->date_of_birth = $dob;
}
private function constructNothing() {
// whatever you need goes here
}
现在您可以调用newemployee()
,并从中获取一个特殊的构造函数
(顺便说一句,如果你调用一个参数太少的函数,PHP不会抱怨;它只会为所有参数传递null
。因此,如果你的新构造函数什么都不做,它会像现在一样工作,因为它只会为每个属性分配null,这是默认值)PHP不支持构造函数重载。下面是一个了解和使用重载构造函数的小技巧。可以使用func_get_args()检索和检查传递的参数,并使用匹配的参数调用自定义重载的_构造函数
class Construct{
function __construct() {
$a = func_get_args(); // get constructor parameters
$i = func_num_args(); // count(func_get_args())
if (method_exists($this,$f='__construct'.$i)) {
/*
* Call to the overloaded __construct function
*/
call_user_func_array(array($this,$f),$a);
}
}
function __construct1($a1) {
echo('__construct with 1 param called: '.$a1);
}
function __construct2($a1,$a2) {
echo('__construct with 2 params called: '.$a1.','.$a2);
}
function __construct3($a1,$a2,$a3) {
echo('__construct with 3 params called: '.$a1.','.$a2.','.$a3);
}
}
$obj = new Construct('one'); // Prints __construct with 1 param
$obj2 = new Construct('one','two'); // Prints __construct with 2 params
$obj3 = new Construct('one','two','three'); // Prints __construct with 3 params
你问的问题让人很困惑。但基本上:要么对象需要某些构造函数参数,要么不需要。如果您希望能够创建可能没有特定数据的对象实例,则需要在构造函数中使这些实例成为可选的。但是,我觉得您在一个类中混合了太多的职责,您可以尝试使用工厂对象,也可以使用func_get_args捕获数量可变的参数。工厂是你最好的选择,我想。。。如果需要对每个方法调用相同的函数,请使用func_get_args或重新考虑。你能描述一下每个构造函数都在做什么吗?。这可能是方法的名称。@deceze Only我必须在数据库表中加载数据并从表中获取数据,因为这是类的意思,对于在一个文件中获取数据,它提供名称/Id作为一个参数,对于加载,它提供所有参数,但文件不同。可能的重复项