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

后端 未结 6 569
死守一世寂寞
死守一世寂寞 2021-02-07 13:48

I am in a situations where i need to instantiate a class with arguments from within an instance of another class. Here is the prototype:

//test.php

class test
{         


        
相关标签:
6条回答
  • 2021-02-07 14:04

    You could use call_user_func_array() I believe.

    or you could leave the arguments list of the constructor, and then inside the constructor use this

    $args = func_get_args();
    
    0 讨论(0)
  • 2021-02-07 14:07
    class textProperty
    {
        public $start;
        public $end;
        function textProperty($start, $end)
        {
            $this->start = $start;
            $this->end = $end;
        }
    
    }
    

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

    don't work?

    0 讨论(0)
  • 2021-02-07 14:09

    you need Reflection http://php.net/manual/en/class.reflectionclass.php

    if(count($args) == 0)
       $obj = new $className;
    else {
       $r = new ReflectionClass($className);
       $obj = $r->newInstanceArgs($args);
    }
    
    0 讨论(0)
  • 2021-02-07 14:18

    We're in 2019 now and we have php7 now... and we have the spread-operator (...) . We can now simply call

    <?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;
        }
    }
    
    
    
    0 讨论(0)
  • 2021-02-07 14:20

    The easiest way I have found:

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

    Sorry a bit raw, but you should understand the idea.

    0 讨论(0)
  • 2021-02-07 14:21

    You can:

    1) Modify test class to accept an array, which contains the data you wish to pass.

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

    2) initiate using a user method instead of the constructor and call it using the call_user_func_array() function.

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

    In your main class:

    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

    Lastly, you can leave your constructor params blank and use 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

    0 讨论(0)
提交回复
热议问题