PHP中__autoload與smarty3衝突的解決方案

來源:互聯網
上載者:User

今天更新了smarty到3.0,結果發現項目中的__autoload()不能用了,

作為一個剛學習PHP 的菜鳥新手,搞了半天才明白問題出在它倆的衝突上,鬱悶了好幾天。後通過查看,Smarty3.0 中的SMARTY2_BC_NOTES檔案,得 知Smarty3.0跟php的__autoload()有衝突:

——————————————————————————-——-

== Autoloader ==

Smarty 3 does register its own autoloader with spl_autoload_register. If your code has

an existing __autoload function then this function must be explicitly registered on

the __autoload stack. See http://us3.php.net/manual/en/function.spl-autoload-register.php

for further details.

——————————— —————————————————————

解決辦法是使用spl_autoload_register()註冊自己的載入類。

現在給出配置前後的代碼:

————————————————————————————————————

  if(substr($className, -6)=="Action"){      include(APP_CLASS_PATH.'Action/'.$className.".class.php");     }elseif(substr($className, -5)=="Model"){      include(APP_CLASS_PATH.'Model/'.$className.".class.php");     }elseif($className=="Smarty"){      require_once NET_ROOT."Smarty/Smarty.class.php";     }elseif(substr($className, -6)=="Public"){    include(APP_CLASS_PATH.'Public/'.$className.".class.php");     }    ——————————————————————————————   class ClassAutoloader {    public function __construct() {     spl_autoload_register(array($this, 'loader'));    }    private function loader($className) {     if(substr($className, -6)=="Action"){      echo '1';      include(APP_CLASS_PATH.'Action/'.$className.".class.php");     }elseif(substr($className, -5)=="Model"){      echo '2';      include(APP_CLASS_PATH.'Model/'.$className.".class.php");     }elseif($className=="Smarty"){      echo '3';      require_once NET_ROOT."Smarty/Smarty.class.php";     }elseif(substr($className, -6)=="Public"){      echo '4';      include(APP_CLASS_PATH.'Public/'.$className.".class.php");     }    }   }   $autoloader = new ClassAutoloader();

希望能協助遇到此問題的朋友,讓大家少走彎路。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.