新版 PHP 預設啟用 APC 模組後造成 class 找不到的錯誤

假設環境:
PHP 5.3.20
Apache/2.2.23 (Unix)

aaa.php 檔案裡面有 AAA class,bbb.php 檔案裡面有 BBB class,bbb.php 中 include aaa.php。BBB class 的 b_func() 函式裡面用到 AAA class (即:$oA = new AAA())。

說明:
APC (Alternative PHP Cache) 是 PHP 的一個延伸模組套件,是 PHP 的另一種快取 (cache) 機制,可以直接拿既有的 OPCODE 執行、免去每次都直譯 php 程式產生 OPCODE 來跑,進而加快 php 程式的執行速度,號稱可以提昇 20% 到 30% 的速度。
筆者將手邊的 PHP 程式轉移到新版 PHP 環境下執行時,PHP 卻拋出「class 找不到」的錯誤訊息,類似以下 log 訊息:

[Tue Jan 29 04:04:46 2013] [error] [client 123.234.171.235] PHP Fatal error: Class ‘AAA’ not found in /sites/prj1/bbb.php on line 32

幾種測試:
1. 命令列模式 (cli) 下跑既有程式 —> OK. 沒錯誤。
2. Apache web server 下執行 bbb.php 程式,把 AAA class 放到 bbb.php 檔案中 (也因此不 include aaa.php 了) —> OK. 沒錯誤。
3. Apache web server 下執行 bbb.php 程式,bbb.php 中 include aaa.php —> Fatal error. 找不到 AAA class。

傻瓜解法:
沒時間深入研究 APC 的各細部參數,所以編輯 /etc/php.d/apc.ini,將 APC 關閉。如下所示:

; This can be set to 0 to disable APC.
apc.enabled=0

存檔後,要記得用 root 重跑 apache server (即:service httpd restart),這樣既有的程式應該就成正常運作了。

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.