|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
walius Smarty n00b
Joined: 10 Jun 2013 Posts: 1
|
Posted: Mon Jun 10, 2013 11:09 pm Post subject: spl_autoload_register with PSR-0 namespacing |
|
|
Hi,
the situation and approach is quite simple: the requrement is to autoload Smarty() class by PSR-0 recomendations, which means that class should have at least one namespace (desired two) like, vendor\Smarty.
All folder structure looks like this:
lib
- vendor
--Smarty
---Smarty.php
(etc)
By PSR-0 class should be requested in this way:
$smart = new \vendor\Smarty\Smarty; (or something with namespaces)
Autoload is conrolled by PSR-0 recomended funcion:
spl_autoload_register (function ($className)
{
$projLib = 'lib/';
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = $projLib . str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
});
The obvious result is an error telling: Fatal error: Class 'vendor\Smarty\Smarty' not found in....
Because of all classes in Smarty are in \root namespace. Adding "namespace vendor\Smarty;" to each file is not a solution as changing the class files is bad practice...
So maybe someone could advice how to have PSR-0 recomended 2 or 1 level namespace for loading Smarty class?
Thanks to everyone in advance! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|