【问题标题】:How to implement php constructor that can accept different number of parameters?如何实现可以接受不同数量参数的php构造函数?
【发布时间】:2011-05-13 01:01:54
【问题描述】:

如何实现可以接受不同数量参数的php构造函数?

喜欢

class Person {
    function __construct() { 
        // some fancy implementation
    } 
} 

$a = new Person('John');
$b = new Person('Jane', 'Doe');
$c = new Person('John', 'Doe', '25');

在 php 中实现此功能的最佳方法是什么?

谢谢, 米洛

【问题讨论】:

  • 构造函数参数的工作方式与任何其他函数的参数一样。只需指定默认值 php.net/manual/en/… 或使用 func_get_args()
  • 构造函数参数的工作方式与任何其他函数的参数一样。只需指定默认值 php.net/manual/en/... 或使用 func_get_args()

标签: php constructor multiple-constructors


【解决方案1】:

一种解决方案是使用默认值:

public function __construct($name, $lastname = null, $age = 25) {
    $this->name = $name;
    if ($lastname !== null) {
        $this->lastname = $lastname;
    }
    if ($age !== null) {
        $this->age = $age;
    }
}

第二种是接受数组、关联数组或对象(关联数组的例子):

public function __construct($params = array()) {
    foreach ($params as $key => $value) {
        $this->{$key} = $value;
    }
}

但在第二种情况下,它应该像这样传递:

$x = new Person(array('name' => 'John'));

第三个选项已被tandu指出:

构造函数参数的工作方式与任何其他函数的参数一样。只需指定默认值 php.net/manual/en/... 或使用 func_get_args()

编辑:把我从original answer tandu(现在:Explosion Pills)中检索到的内容粘贴到这里。

【讨论】:

  • @Tadeck,您到 tandu 的链接现在转到 Explosion Pills 开发者页面,不清楚答案页面从哪里来。如果您还记得第三个答案是什么,您可以将其粘贴到您的答案更新中吗?
  • @JesseChisholm:这是同一用户(链接基于用户 ID,此用户只是更改了昵称)。是的,第三个答案对我来说仍然是可见的,但被版主删除(如果你能看到的话,链接是:stackoverflow.com/a/5986259/548696)。将他/她的答案内容添加到我的答案中。
【解决方案2】:

更新答案:

echo '<pre>';

// option 1 - combination of both tadeck's and my previous answer

class foo {
    function __construct() {
        $arg_names = array('firstname', 'lastname', 'age');
        $arg_list = func_get_args();
        for ($i = 0; $i < func_num_args(); $i++) {
            $this->{$arg_names[$i]} = $arg_list[$i];
        }
    }
}

$foo = new foo('John', 'Doe', 25);

print_r($foo);

// option 2 - by default, PHP lets you set arbitrary properties in objects, even
// if their classes don't have that property defined - negating the need for __set()

// you will need to set properties one by one however, rather than passing them as
// parameters

class bar {
}

$bar = new bar();
$bar->firstname = 'John';
$bar->lastname = 'Doe';
$bar->age = 25;

print_r($bar);

结果:

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)

上一个答案:

<?php

class Person {
    function __construct() {
        $arg_list = func_get_args();
        echo '<p>';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'<br />', "\n";
        }
    }
}

$a = new Person('John');
$b = new Person('Jane', 'Doe');
$c = new Person('John', 'Doe', '25');

?>

结果:

Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25

【讨论】:

  • 另一种选择是在 PHP 中使用重载 (_set)
  • 您能否详细说明如何使用_set() 重载来实现类似的效果?
  • @Tadeck:我已经重构了我的答案,尽管 __set() 并不是真正必要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2013-02-23
相关资源
最近更新 更多