简体   繁体   English

如何将一组变量传递给类

[英]How to pass a set of variables to a class

This may be a bit of an XY questions, so I'm going to explain what I'm trying to do first. 这可能是一个XY问题,所以我将解释我首先要做的事情。 I'm attempting to create a single php file to handle all of my page refresh AJAX calls. 我试图创建一个php文件来处理我所有的页面刷新AJAX调用。 That means I want to be able to send it a class name, plus a list of the variables that the class constructor takes, and for it to then create the class. 这意味着我希望能够向它发送一个类名,再加上一个类构造函数使用的变量列表,然后再为其创建类。

I can create the class fine. 我可以创建良好的类。 $class = new $className(); works just fine for creating the class. 对于创建类来说效果很好。 The problem is passing in the default variables. 问题是传入默认变量。 Most of the variables are objects containing other classes, so I can't just include this once the class is created, I need to pass them as the class is created. 大多数变量是包含其他类的对象,因此,一旦创建了类,我就不能只包含它,我需要在创建类时传递它们。

I was thinking something along the lines of: 我在考虑以下方面:

$varStr = '';
$s = '';
foreach($vars as $var) {
    switch($var['type']) {
        case 'object':
            $varStr .= $s . '$' . $var['value'];
            break;
        case 'variable':
            $varStr .= $s . $var['value'];
    }
    $s = ',';
}
$class = new $className(echo $varStr);

Now obviously echo $varStr isn't going to work there, but I have no idea what will. 现在,显然echo $ varStr不会在那里工作,但是我不知道会怎样。 Is there anything I can do that will output the variables from my array into the class constructor like that? 我有什么办法可以将数组中的变量输出到类构造函数中? Is what I'm trying to do even possible? 我要做什么甚至有可能吗? Is there a better way? 有没有更好的办法?

Whilst I understand I could just pass the whole array to the class constructor, this would complicate the main part of the program, and I would rather just ditch the idea of a single page for AJAX refresh than go down that route. 虽然我知道我可以将整个数组传递给类构造函数,但这会使程序的主要部分复杂化,我宁愿放弃AJAX刷新的单个页面的想法,也不愿沿这条路线走。

So basically you're trying to pass a variable number of arguments to a constructor? 因此,基本上,您正在尝试将可变数量的参数传递给构造函数? In a regular function, you could do something like: 在常规函数中,您可以执行以下操作:

function foo() {
    $args = func_get_args();
    ...
}

call_user_func_array('foo', array('bar', 'baz'));

This won't work for constructors, since the calling mechanism is different. 这对构造函数不起作用,因为调用机制不同。 You could do: 您可以这样做:

class Foo {

    public function __construct() {
        $args = func_get_args();
        ...
    }

}

$class = new ReflectionClass('Foo');
$obj = $class->newInstanceArgs(array('bar', 'baz'));

But really, what you should be doing is this: 但实际上,您应该做的是:

class Foo {

    public function __construct(array $args) {
        ...
    }

}

$obj = new Foo(array('bar', 'baz'));

or 要么

class Foo {

    public function __construct($bar, $baz) {
        ...
    }

}

$obj = new Foo('bar', 'baz');

Anything else is quite insane. 其他一切都是疯狂的。 If your object constructor is so complicated, you probably need to simplify it. 如果您的对象构造函数非常复杂,则可能需要简化它。

This is a wild guess at what you're trying to do but maybe this is what you're after: 这是对您要做什么的一个疯狂猜测,但是也许这是您追求的:

// Generate constructor args

$args = array();
foreach($vars as $var) {
    switch($var['type']) {
        $value = $var['value'];
        case 'object':
            args[] = ${$value};     // evaluate, I think that's what you want?
            break;
        case 'variable':
            args[] = $value;        // use as is
            break;
    }

}

// Instanciate class with args
$class = new ReflectionClass($className);
$obj = $class->newInstanceArgs($args);

For this to work, it would require $vars to enumerates args in the correct order expected by each class constructor. 为此,需要$ vars以每个类构造函数期望的正确顺序枚举args。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM