说实话,我认为你不能以一种直截了当的方式.无论如何,使用实用程序类不是更好吗? OOP没什么,看看这个:
class HTMLUtil {
public static function filter($str) {...}
public static function entities($str) {...}
public static function encode($str) {...}
...etc...
}
?>
将相关功能组合在一起的静态助手/实用程序类很容易组合在一起,只需将您的函数声明为static:
Declaring class members or methods as
static makes them accessible without
needing an instantiation of the class.
现在你可以使用__autoload了.您不必实例化这些类以使用它们的任何静态函数,它使您的代码更具可读性(如果稍微冗长一点).我总是觉得做以下事情更令人满意:
echo HTMLUtil::filter($str);
代替:
echo filter($str); //scary filter function, where are you from?
如果需要,您还可以在实用程序类中声明一个私有构造函数,以防止它们被实例化,以强调它们的“只是一堆相关函数”:
private __construct() {...}
要从同一个类中的另一个函数调用静态函数,您可以使用self关键字(引用相同的类,而不是对象或类实例)来执行此操作:
public static function foo()
{
echo self::bar() . '..and some foo';
}