PHP:如何使用另一个类中的参数实例化一个类

时间:2009-12-07 06:39:57

标签: php oop class object instantiation

我处于需要使用另一个类的实例中的参数实例化类的情况。 这是原型:

//test.php

class test
{
    function __construct($a, $b, $c)
    {
        echo $a . '<br />';
        echo $b . '<br />';
        echo $c . '<br />';
    }
}

现在,我需要使用以下类的 cls 函数来实例化上面的类:

class myclass
{
function cls($file_name, $args = array())
{
    include $file_name . ".php";

    if (isset($args))
    {
        // this is where the problem might be, i need to pass as many arguments as test class has.
        $class_instance = new $file_name($args);
    }
    else
    {
        $class_instance = new $file_name();
    }

    return $class_instance;
}
}

现在,当我尝试在向其传递参数时创建测试类的实例时:

$myclass = new myclass;
$test = $myclass->cls('test', array('a1', 'b2', 'c3'));

它给出错误: 缺少参数1和2;只传递第一个参数。

如果我实例化一个在它的构造函数中没有参数的类,这可以正常工作。

对于有经验的PHP开发人员来说,上面应该不是什么大问题。请帮忙。

由于

6 个答案:

答案 0 :(得分:28)

您需要反思http://php.net/manual/en/class.reflectionclass.php

if(count($args) == 0)
   $obj = new $className;
else {
   $r = new ReflectionClass($className);
   $obj = $r->newInstanceArgs($args);
}

答案 1 :(得分:4)

你可以:

1)修改测试类以接受一个数组,该数组包含您希望传递的数据。

//test.php

class test
{
        function __construct($a)
        {
                echo $a[0] . '<br />';
                echo $a[1] . '<br />';
                echo $a[2] . '<br />';
        }
}

2)使用用户方法而不是构造函数启动并使用call_user_func_array()函数调用它。

//test.php

class test
{
        function __construct()
        {

        }

        public function init($a, $b, $c){
                echo $a . '<br />';
                echo $b . '<br />';
                echo $c . '<br />';
        }

}

在你的主要课程中:

class myclass
{
function cls($file_name, $args = array())
{
        include $file_name . ".php";

        if (isset($args))
        {
                // this is where the problem might be, i need to pass as many arguments as test class has.
                $class_instance = new $file_name($args);
                call_user_func_array(array($class_instance,'init'), $args);
        }
        else
        {
                $class_instance = new $file_name();
        }

        return $class_instance;
}
}

http://www.php.net/manual/en/function.call-user-func-array.php

最后,您可以将构造函数参数留空并使用func_get_args()

//test.php

class test
{
        function __construct()
        {
                $a = func_get_args();
                echo $a[0] . '<br />';
                echo $a[1] . '<br />';
                echo $a[2] . '<br />';
        }
}

http://sg.php.net/manual/en/function.func-get-args.php

答案 2 :(得分:1)

我相信你可以使用call_user_func_array()

或者你可以保留构造函数的参数列表,然后在构造函数中使用这个

$args = func_get_args();

答案 3 :(得分:1)

class textProperty
{
    public $start;
    public $end;
    function textProperty($start, $end)
    {
        $this->start = $start;
        $this->end = $end;
    }

}

$ object = new textProperty($ start,$ end);

不起作用?

答案 4 :(得分:0)

我找到的最简单方法:

if ($depCount === 0) {
            $instance = new $clazz();
        } elseif ($depCount === 1) {
            $instance = new $clazz($depInstances[0]);
        } elseif ($depCount === 2) {
            $instance = new $clazz($depInstances[0], $depInstances[1]);
        } elseif ($depCount === 3) {
            $instance = new $clazz($depInstances[0], $depInstances[1], $depInstances[2]);
        }
抱歉有点生气,但你应该明白这个想法。

答案 5 :(得分:0)

我们现在在2019年,现在有了php7 ...,我们有一个算子(...)。 现在,我们可以简单地致电

<?php

class myclass
{
    function cls($file_name, $args = array())
    {
        include $file_name . ".php";

        if (isset($args))
        {
            $class_instance = new $file_name(...$args); // <-- notice the spread operator
        }
        else
        {
            $class_instance = new $file_name();
        }

        return $class_instance;
    }
}