Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

solving __autoload() with your own

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Feb 09, 2010 12:27 pm    Post subject: solving __autoload() with your own Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Feb 09, 2010 2:24 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks 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