16

別のクラスのインスタンス内から引数を使用してクラスをインスタンス化する必要がある状況にいます。プロトタイプは次のとおりです。

//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 開発者にとって、上記は大きな問題にはなりません。助けてください。

ありがとう

4

6 に答える 6

33

リフレクションが必要http://php.net/manual/en/class.reflectionclass.php

if(count($args) == 0)
   $obj = new $className;
else {
   $r = new ReflectionClass($className);
   $obj = $r->newInstanceArgs($args);
}
于 2009-12-07T07:30:15.280 に答える
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

于 2009-12-07T06:42:47.813 に答える
1
class textProperty
{
    public $start;
    public $end;
    function textProperty($start, $end)
    {
        $this->start = $start;
        $this->end = $end;
    }

}

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

動作しませんか?

于 2011-11-12T02:28:13.203 に答える
1

call_user_func_array()を使用できると思います。

または、コンストラクターの引数リストをそのままにして、コンストラクター内でこれを使用することもできます

$args = func_get_args();
于 2009-12-07T06:44:13.613 に答える
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]);
        }

少し生々しく申し訳ありませんが、その考えを理解する必要があります。

于 2017-06-02T13:30:06.060 に答える
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;
    }
}


于 2019-03-20T10:30:18.743 に答える