This function can take the place of the new keyword for instantiating classes. Currently only primitive types can be passed as argument parameters. I was trying to figure out a way to pass arrays in the argument list using func_get_args() and eval() but can't think of way to produce the constructor argument list.
define( 'SYSTEMROOT', 'path/to/documentroot' );
/**
* PHP 4 class autoloading function
* @param $classname The name of the class to instantiate
* @param $path The path to the file containing the class, dot separated a la Java
* @param $params Array of parameters to pass to the class constructor. Primitive types only
* @return object
*/
function &cl( $classname, $path, $params = null )
{
if( !class_exists( $classname ))
{
require_once( SYSTEMROOT . '/' . str_replace( '.', '/', $path ) . '.php' );
}
if( $params != null )
{
$params = implode( ',', $params );
eval( "\$object =& new $classname( $params );" );
return $object;
}
return new $classname;
}
$foo =& cl( 'mySqlDataLayer', 'classes.database.mysql', array( 1, 2 ));