簡體   English   中英

PHP:如何使用另一個類中的參數實例化一個類

[英]PHP: How to instantiate a class with arguments from within another class

我處在需要使用另一個類實例中的參數實例化一個類的情況下。 這是原型:

//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開發人員來說,上述問題不應該太大。 請幫忙。

謝謝

您需要反射http://php.net/manual/zh/class.reflectionclass.php

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

您可以:

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/zh/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

我相信您可以使用call_user_func_array()

或者您可以離開構造函數的參數列表,然后在構造函數內部使用此方法

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

}

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

不工作嗎

我找到的最簡單的方法是:

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

不好意思,但是您應該了解這個想法。

我們現在是2019年,現在有了php7 ...並且我們擁有spread-operator(...)。 現在我們可以簡單地致電

<?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;
    }
}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM