Sticky Posts
Jun 17, 2009
Creating PHP Classes With Dynamic Parameters
I was designing a one-size-fits-all singleton class and function which I will publish after a bit of testing but bumped into a major issue on the way. As we know PHP provides call_user_func_array to call any function with an array of parameters. Unfortunately you can not use such function to create a new class.
Let's say you want to create a new $class instance. $class is a variable class name and creating it is as easy as saying $obj = new $class(); Ok. If you need parameters added to the equation you get to $obj = new $class($param1, $param2, ...); All cool! But what if you don't know the parameters and you need to use an array.
This is where it gets messy as I have not found any way to do this in PHP. You could call $obj = new $class(array($param1, $param2, ...)); but you need to understand this is not a one-size-fits-all solution and the class __constructor will need to be altered to take just one parameter ... an array ... and extract the other parameters from it. It's not a difficult task but if would require changing virtually any class you need to instantiate like this. So this alternative falls.
Some may think you could instantiate the class: $obj = new $class(); and then call_user_func_array(array($obj, '__construct'), $params); but this will call the __constructor twice which, for any real coder, is unacceptable.
As I was pondering upon the given situation I thought of a solution I will share here with you. It can instantiate a new PHP class with a variable parameter count which can be provided as an array. It uses some eval magic (not evil magic) and works great for my needs. This function will allow you to achieve the following lines of code.
<?php
// Use with known parameters
$obj = el_php_newClass('TestClass', 1, 2, 3);
var_dump($obj); // Validate return
// Or with a dynamic array
$obj = el_php_newClassArray('TestClass', array(1, 2, 3));
var_dump($obj); // Validate return
?>
After I wrote the eval version I tried the Reflection version too using ReflectionClass and newInstance. And this one worked too :) So the functions have two versions. One with reflection and one with eval. Make your choice or, better, let code choose for itself.
The Actual Script
You will see the functions validate everything. Class name/existence and parameters too. If something cracks, it returns a NULL.
Registration is FREE, quick, painless and worth its weight in gold.
And this is a simple test:
<?php
// ---------------------------------------------------------------------- //
class TestClass{
function TestClass(){
// Old school constructor
$args = func_get_args();
call_user_func_array(array($this, '__construct'), $args);
}
function __construct(){
$args = func_get_args(); // Get Args
print_r($args); // Show args
}
}
// ---------------------------------------------------------------------- //
// Use with known parameters
$obj = el_php_newClass('TestClass', 1, 2, 3);
var_dump($obj);
// Or with a dynamic array
$obj = el_php_newClassArray('TestClass', array(1, 2, 3));
var_dump($obj);
// ---------------------------------------------------------------------- //
?>
If you have questions, comment form is lower down the page.


