tags:

views:

224

answers:

3

I have a "best practice" question. I'm developing a OO MVC framework in PHP, and most of the classes interact easily - they are literally declared in the code and used. For instance:

// In class 'getDetails'
$db = new mysqli(.....);
$db->query(.....);

However, there are times when the class and function names are dynamically built. The actual class files are all created and located in the framework somewhere, but they are not all literally declared and used. It isn't until run time that the framework knows what class it needs to complete the request; so the class and function names are usually created and stored in variables. In the simplest case, the variables are used to create an object and run a function. Example:

$request = 'blog'; 
$action = 'view';
$class = new $request(); // Creates an blog object
$class->$action(); // Runs the blog function view

However, I have already run into a situation when trying to use variables to run static functions (here is the stack overflow question and answer) where the variables couldn't be used in the literal usage
( $request::$action() gives parse errors). I have seen in the PHP manual functions for interacting/using classes, functions, and objects, but have not had to deal with them before.

My question is what is the best way to handle and run classes and functions where the class and function names are created on the fly?

A: 

I'm not sure if I understand you well, but you could use the factory design pattern.

tomp
A: 

If I understand your question correctly, then I would use eval for this. You can also do this for static functions. For example, here is some code from a site I built a few years ago.

function StaticObjectPrint($mysql,$template, $objecttype, $debug=0) {
    foreach(eval("return $objecttype::ReturnStaticVariablesForPrinting(\$mysql);") as $key => $value) {
        global $$key;
        $$key = $value;
    }
    if($debug==1)
        print_r ($GLOBALS);

    return ParsePlainTextFile($template,1);
}

In this case, there were several different classes of objects, but they all had a ReturnStaticVariablesForPrinting function. Depending on which type of object a user clicked on, it would return some static information about that object, without having to instantiate each object (and tie up a bunch of memory). This was useful for producing lists of object properties. (I realize this violates some OO principles, but it was necessary for speed)

Scott Lundberg
+3  A: 

Both methods you mentioned are both good but have some limitations:

Using the regular notation:

$request = 'blog'; 
$action = 'view';
$class = new $request(); // Creates an blog object
$class->$action(); // Runs the blog function view

Using the notation for instantiating classes requires that you know in advance the parameters each class/method accepts. So you cannot design a factory pattern with it that will accept arbitrary parameters.

Using call_user_func_array() allows you to use arbitrary parameters.

$request = 'blog'; 
$action = 'view';
$params = array(
   $_GET['category'],
   $_GET['limit']
);
call_user_func_array(array($request, $action), $params);

So the above code is equivalent to the literal:

blog::view($_GET['category'], $_GET['limit']);

Basically, call_user_func_array() flattens the array $params, passing each value in it as parameters to the method blog::view().

To do the same with dynamic/object method call:

call_user_func_array(array(new $request, $action), $params);

However, this does not solve the problem with creating an instance of an arbitrary class, and passing it an arbitrary number of parameters. To do this you can use the ReflectionClass.

Example:

$request = 'blog';
$action = 'view';
$configs = array('something', 'something else');
$params = array(
   $_GET['category'],
   $_GET['limit']
);
$instance = call_user_func_array(
   array(new ReflectionClass($request), 'newInstance'), 
   $configs
);
$return = call_user_func_array(array($instance, $action), $params);

This would be equivalent to:

$configs = array('something', 'something else');
$params = array(
   $_GET['category'],
   $_GET['limit']
);
$blog = new blog($configs[0], $configs[1]);
$blog->view($_GET['category'], $_GET['limit']);

With those tools you can dynamically instantiate arbitrary Objects and pass arbitrary number of parameters to their __constructor() as well as any method.

If you meant best in terms of functionality, use call_user_func_array() and ReflectionClass(). If you meant best in terms of performance, don't worry about it. Good design and functionality improves performance more.

bucabay