2013-02-08 34 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这样做。