2013-02-08 32 views
0

我一直遇到問題,我想使用一個有用的功能,如str_getcsv()quoted_printable_encode(),但發現它僅在PHP 5.3之間出現。我想讓我編寫的代碼至少兼容5.2,但我不想編寫一堆專門的代碼。__autoload()等價於非類功能?

我已經從PHP.net註釋中獲得了習慣抓取替身函數,將它們存儲在如func.quoted_printable_encode.php這樣的有用名稱的文件中,並在每個我想要調用它們的地方編寫如下所示的塊。

if(! function_exists('quoted_printable_encode')) { 
    $funcfile = 'func.quoted_printable_encode.php'; 
    if(file_exists($funcfile) && is_readable($funcfile)) { 
     require('func.quoted_printable_encode.php'); 
    } else { 
     Throw new Exception('Cannot invoke function: quoted_printable_encode'); 
    } 
} 

這似乎極其相似__autoload()上課,併爲__call()對象的方法,但對於全局函數我無法找到任何東西。這樣的事情是否存在,還是我必須將所有這些額外的功能都放在某個頭文件中?

回答

1

無法在PHP中自動加載函數。有an RFC但它尚未實現(它可能不會)。

如果你真的想自動加載函數,那麼創建一個幫助器類來實現函數作爲靜態方法是有意義的。它是在框架中做的quitecommon這樣做。