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?
Both methods you mentioned are both good but have some limitations:
Using the regular notation:
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.
So the above code is equivalent to the literal:
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:
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:
This would be equivalent to:
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.