Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
spl_autoload_register with PSR-0 namespacing

 
Post new topic   Reply to topic    Smarty Forum Index -> Installation and Setup
View previous topic :: View next topic  
Author Message
walius
Smarty n00b


Joined: 10 Jun 2013
Posts: 1

PostPosted: Mon Jun 10, 2013 11:09 pm    Post subject: spl_autoload_register with PSR-0 namespacing Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Installation and Setup All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP