我試圖決定它是否適合使用類聲明一組〜20個公共功能:應該使用PHP類嗎?
if (!class_exists('example')) {
class example {
# Declare ~20 functions (methods), all of
# which are public. (There's nothing else
# in the class.)
public static function one() { /* ... */ }
public static function two() { /* ... */ }
# ...
} # class
}
使得可作爲方法:
example::one();
example::two();
什麼優點/缺點是出現以上方法vs。只是這樣做:
if (!defined('EXAMPLE_LOADED')) {
define('EXAMPLE_LOADED', true);
function example_one() { /* ... */ }
function example_two() { /* ... */ }
# ...
}
使可用的功能爲:
example_one();
example_two();
編輯 - 相關的性能測試:
- function calls from global/class/namespace
- function calls in classes, namespaces
- properties vs. global variables vs. constants
如果使用足夠高的PHP版本,您還可以對它們進行命名空間。 –
你應該很可能不會爲此使用類。不應將類用作任意函數的「容器」。 –
您可能會更好地使用[namespaces](http://us.php.net/namespaces) - 它取決於您使用的PHP版本。 –