Smarty 3 和 __autoload() 引起的問題

 

When I first tried out Smarty 3 (which is available as Release Candidate 3 at the moment), which was that I updated an existing Smarty 2 installation, I ran into this error message:

Fatal error: Class ‘Smarty_Internal_Template’ not found in lib/Smarty/Smarty.class.php on line 444

I started to investigate what the problem may be and soon found out that libraries in Smarty/sysplugins/ were not loading. So I picked up my already existing __autoload() function and added the following code:

function __autoload() {
  // other stuff that had been here before

  if (substr($classname, 0, 15) == "Smarty_Internal") {
    $classname = "Smarty/sysplugins/" . strtolower($classname);
    require_once $classname . ".php";
  }
}

With this addition, Smarty 3 worked perfectly, but I still kept wondering why I had to tell my autoload function explicitly to load the Smarty_Internal libraries. I found it quite ugly that I had to add it myself.

So I asked in the Smarty forum and here is why: Smarty registers its own autoload function, using the spl_autoload_register() function. Having my own __autoload() function breaks this, unless I register my own function with spl_autoload_register() as well. So the solution looks like this:

function autoload() {
  // my old stuff, but no more loading of Smarty libraries
}

spl_autoload_register("autoload");

Very simple solution, if you just get the right hint. So maybe if somebody stumbles upon the same error message, this may be of help to resolve this nicely and quickly.

If you are interested in seeing my post in the Smarty forum, it can be found athttp://www.smarty.net/forums/viewtopic.php?p=65975.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章