views:

1407

answers:

3

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

?>
+2  A: 

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 :)

The Pixel Developer
I got the same idea as well but I was curious to know if something like I described could be done.
Alix Axel
+1  A: 

I haven't tried this, but call_user_func_array sounds like you want.

$thing = call_user_func_array(array($classname, '__construct'), $args);

Have a look in the PHP Documentation.

staticsan
Doesn't work. =\call_user_func_array(array(new $classname(), '__construct'), $args);This works but then I would be calling the constructor twice.
Alix Axel
Try it without new $classname().
staticsan
Fatal error: Non-static method test::__construct() cannot be called statically
Alix Axel
Okay, I've got no more ideas. Thanks for being willing to try.
staticsan
+4  A: 
$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?

troelskn
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
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?
Alix Axel
It's not you: The documentation is rather sparse on that. You'll have to do with http://www.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
troelskn