I know I need to use spl_autoload_register with Smarty 3. I am registering my autoload function after smarty initializes. But smarty is trying to use my own autoload function instead of the smartyAutoload defined function. Causing an error because it obviously cant find the smarty files using my autoload. Here is the code with everything else cut out to show how it is layed out currently.
I'm sure it is just an order placement issue or something.
<?php
class application {
// include smarty
require_once(SMARTY_DIR.'Smarty.class.php');开发者_如何转开发
// init controller class which initializes smarty
$controller = new Controller();
}
function autoLoader($class) {
// determine what type class it is and call from that directory
$dir = strtolower(strstr($class, '_', true));
$name = substr( strtolower( strstr($class, '_') ), 1 );
switch($dir) {
case 'component':
break;
default:
require_once(LIB_PATH.DS.$name.'.class.php');
break;
}
}
spl_autoload_register("autoLoader");
?>
You can make your autoloader compatbile (that's what spl_autoload_register
is actually for) by only require a file if it's one of your classes.
You can do this by checking the class name against a namespace or a whitelist or with is_file
(store the whitelist in the file-system):
default:
$path = LIB_PATH.DS.$name.'.class.php';
if (is_file($path)) require_once($path);
break;
I don't know if it's relevant to you, but I tracked my problem down to a file called smarty_internal_templatecompilerbase.php
, function callTagCompiler()
.
My code (inside a *.tpl file) calls a smarty plugin function. My plugin-function is called Load_PO
, meaning that the Smarty engine then searches for a class named Smarty_Internal_Compile_Load_PO
.
Of course, neither the Smarty auto-loader, nor my custom auto-loader finds this template. The problem, in fact, was that my auto-loader errors when a file is not found.
The problem can be resolved in 2 ways:
Change my auto-loader to ignore searches
if (substr($class_name,0,24) = 'Smarty_Internal_Compile_')
Modify file
smarty_internal_templatecompilerbase.php
, and change functioncallTagCompiler()
as shown below
Change the code
class_exists ($class_name)
To
class_exists ($class_name, false)
精彩评论