Passing arguments to the Class Constructor

12,940

Solution 1

$klass = new ReflectionClass($classname);
$thing = $klass->newInstanceArgs($args);

Although the need to use reflection suggests that you are overcomplicating something in your design. Why do you want to write this function in the first place?

Solution 2

Instead of your class taking separated parameters I'd have it take an array.

class DB
{
    public function __construct(array $params)
    {
        // do stuff here
    }
}

That way you can pass the direct result of the func_get_args into your constructor. The only problem now is being able to figure out the array key / values.

If anyone else has any ideas I'd also be delighted to know :)

Share:
12,940
Alix Axel
Author by

Alix Axel

If you need to, you can contact me at: alix [dot] axel [at] gmail [dot] com. I'm #SOreadytohelp Some of my GitHub repositories: phunction, a minimalistic PHP HMVC Framework. halBox, bash script to bootstrap Debian/Ubuntu servers. ArrestDB, RESTful API for SQLite, MySQL and PostgreSQL databases. genex.js, Genex module for Node.js. If you know how to work with regexes, have a look at http://namegrep.com/. ;)

Updated on June 19, 2022

Comments

  • Alix Axel
    Alix Axel almost 2 years

    How can I pass a arbitrary number of arguments to the class constructor using the Object() function defined below?

    <?php
    
    /*
    ./index.php
    */
    
    function Object($object)
    {
        static $instance = array();
    
        if (is_file('./' . $object . '.php') === true)
        {
            $class = basename($object);
    
            if (array_key_exists($class, $instance) === false)
            {
                if (class_exists($class, false) === false)
                {
                    require('./' . $object . '.php');
                }
    
                /*
                How can I pass custom arguments, using the
                func_get_args() function to the class constructor?
    
                $instance[$class] = new $class(func_get_arg(1), func_get_arg(2), ...);
                */
    
                $instance[$class] = new $class();
            }
    
            return $instance[$class];
        }
    
        return false;
    }
    
    /*
    How do I make this work?
    */
    
    Object('libraries/DB', 'DATABASE', 'USERNAME', 'PASSWORD')->Query(/* Some Query */);
    
    /*
    ./libraries/DB.php
    */
    
    class DB
    {
        public function __construct($database, $username, $password, $host = 'localhost', $port = 3306)
        {
            // do stuff here
        }
    }
    
    ?>
    
  • Alix Axel
    Alix Axel about 15 years
    I got the same idea as well but I was curious to know if something like I described could be done.
  • Alix Axel
    Alix Axel about 15 years
    Doesn't work. =\ call_user_func_array(array(new $classname(), '__construct'), $args); This works but then I would be calling the constructor twice.
  • Deva
    Deva about 15 years
    Try it without new $classname().
  • Alix Axel
    Alix Axel about 15 years
    I admit this function may seem a bit nonsense but I use it for small stuff where a framework may be too bloated. And since it acts as an "autoloader", object constructor and singleton container it does the job perfectly.
  • Alix Axel
    Alix Axel about 15 years
    BTW, that Reflection class is really handy, is there any crash course available on the new features of PHP 5.3? I checked the PHP manual but is it me or it is lacking a lot of documentation?
  • troelskn
    troelskn about 15 years
    It's not you: The documentation is rather sparse on that. You'll have to do with php.net/oop5.reflection. Also, it has been around since 5.1 (or is it 5.0?), so it's not a new feature of 5.3
  • Alix Axel
    Alix Axel about 15 years
    Fatal error: Non-static method test::__construct() cannot be called statically
  • Deva
    Deva about 15 years
    Okay, I've got no more ideas. Thanks for being willing to try.
  • Addo Solutions
    Addo Solutions about 11 years
    Eval is pretty much always a bad idea
  • Ahmed Aboud
    Ahmed Aboud over 4 years
    if you doing this in a dynamic way use type hints for your arguments in the class constructor