gpt4 book ai didi

php - 如何在函数中传递参数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:58:10 24 4
gpt4 key购买 nike

我有这样的功能。

function load($name, $arg1, $arg2, $arg3, $arg4){
$this->$name = new $name($arg1, $arg2, $arg3, $arg4);
}

load() 方法将加载一些类并将其设置为类 property 和无限参数,取决于它们分配的类。

另一个例子,如果我只用 3 个参数设置方法 $this->load,这个过程中会发生什么

function load($name, $arg1, $arg2){
$this->$name = new $name($arg1, $arg2);
}

有可能做这样的事情吗?

最佳答案

您可以使用 func_get_args() 的组合, ReflectionClass以及 this comment 的帮助像这样:

function load(){
$args = func_get_args();
if( !count( $args)){
throw new Something();
}

$name = array_shift( $args);
$class = new ReflectionClass($name);
$this->$name = $class->newInstanceArgs($args);
}

关于php - 如何在函数中传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9281986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com