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

发布于 2024-11-06 18:04:36 字数 299 浏览 2 评论 0原文

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

就像

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

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

在 php 中实现这个的最佳方法是什么?

谢谢, 米洛

How to implement php constructor that can accept different number of parameters?

Like

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

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

What is the best way to implement this in php?

Thanks,
Milo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-11-13 18:04:36

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

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()

编辑:粘贴在这里我能够从tandu的原始答案中检索到的内容(现在:Explosion药丸)。

One solution is to use defaults:

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

The second one is to accept array, associative array or object (example about associative array):

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

But in the second case it should be passed like this:

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

The third option has been pointed by tandu:

Constructor arguments work just like any other function's arguments. Simply specify defaults php.net/manual/en/… or use func_get_args().

EDIT: Pasted here what I was able to retrieve from original answer by tandu (now: Explosion Pills).

幻想少年梦 2024-11-13 18:04:36

更新的答案:

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

Updated Answer:

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);

Result:

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

Previous Answer:

<?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');

?>

Result:

Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文