I suggested something like this earlier, but for Zend_Registry not Zend.
I think enhance Zend_Registry but not the Zend class. The enhancements I
proposed allowed you to load and instantiate an object with a single
call and also allowed registering a class name and instantiation
parameters so that you could Lazy Load the object (in the Action for
example). I believe that Sheakoski suggested adding getInstanceOf()
functionality as well.
Simon Mundy wrote:
Perhaps an extra method to Zend would be helpful -
Zend::loadComponent(). It would allow you to assemble the name of a
class with a prefix, use Zend::loadClass to ensure it exists and
returns either a formatted class name or false.
E.g. If I have a $dbtype with 'PDO_MYSQL', 'pdoMysql' or 'pdo_mysql'
as the adapter, I could call:-
$adapter = Zend::loadComponent($dbtype, 'Zend_Db');
$db = new $adapter($config);
I don't see it as any more bloat than existing factory methods and
provides the convenience of preserving the underscore convention of
classes without imposing overly-tight restrictions on your data/config.
Quick example:-
static public function loadComponent($class, $prefix = null)
{
$class = self::formatClassName($prefix . '_' . $class);
self::loadClass($class);
return $class;
}
static public function formatClassName($class)
{
$class = preg_replace('/([a-z])([A-Z])/', "$1_$2", $class);
$class = trim(ucwords(str_replace('_', ' ', strtolower($class))));
$class = str_replace(' ', '_', $class);
return $class;
}
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124
http://www.peptolab.com