Page 1 of 1

Calling Functions with Arg List

Posted: Sun Aug 27, 2006 2:01 pm
by LiveFree
hello All,

With my newest project, I am trying to use OOp more and more. Right now I have an 'plugin' loader that takes a class name as input and includes the class file and returns an instance.

The problem is I have a DB Abstraction layer that I wrote that takes the connection values as a __Constructor values.

Code: Select all

public function getComponent($className) {
	if (!file_exists(dirname(__FILE__) . '/' . $className . '.class.php')){
	return false;
		}else{
			if ($className == 'mysql'){
		require(dirname(__FILE__) . '/mysql.class.php');
	return new DB('host', 'user', 'pass', 'db');
			}else{
		require(dirname(__FILE__) . '/' . $className . '.class.php');
	return new $className;
		}
	    }
	}
So right now I have to check if the class name is MySQL and manually input the strings.

How can I use func_get_args to create an instance of the specified class with all the arguments passed to the 'getComponent'

Like

Code: Select all

$base->getCompoment('mysql', 'localhost', 'root', '', 'bla');
Thanks!

Posted: Sun Aug 27, 2006 2:04 pm
by feyd
:?

Code: Select all

$obj = new $className();
call_user_func_array(array($obj, 'getComponent'), func_get_args());