Calling Functions with Arg List

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Calling Functions with Arg List

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:?

Code: Select all

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