|
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 |
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Tue Feb 09, 2010 12:27 pm Post subject: solving __autoload() with your own |
|
|
As Smarty (new version) tries to make use of autoload function ( spl_autoload_functions ), it creates a problem if you have to define your own __autoload() function.
So, in this case, you can re-write / adjust its autoload within your __autoload() function, and it just works good at the moment.
What you should do? In Smarty.class.php, find the below code and remove it.
Code: | if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
$registeredAutoLoadFunctions = spl_autoload_functions();
if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
spl_autoload_register();
}
} else {
spl_autoload_register('smartyAutoload');
} |
Then, modify your own __autoload() with something similar to:
Code: | function __autoload($class_name='')
{
$class_name = strtolower($class_name);
if(substr($class_name, 0, 16) === 'smarty_internal_' || $class_name == 'smarty_security')
{
include __ROOT__ . '/library/classes/third/Smarty-3.0b7/sysplugins/' . $class_name . '.php';
return;
}
...
(rest of your code goes here)....
}
|
return; is an important issue, because, once your __autoload() knows that it was a Smarty's class and its file is included, you just have to quit the rest of the code.
This makes you upgrade to new version of Smarty with no compains with __autoload issue.
Thanks. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Feb 09, 2010 2:24 pm Post subject: |
|
|
That is not a good way, there is no need to edit the Smarty class. You shouldn't need to use __autoload(), you should instead register your autoloader with spl_autoload_register().
First, define this before including Smarty:
Code: | define('SMARTY_SPL_AUTOLOAD',1); |
Then, create your own autoload function:
Code: | function myAutoload() { ... } |
Then, register your autoloader:
Code: | spl_autoload_register('myAutoload'); |
Smarty will then add its own autoloader along with your own. |
|
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
|