小编Rob*_*Rob的帖子

PHP - Zend说避免魔术方法?

我正在阅读这个页面 - http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited

其中一个建议是避免使用Zend Performance PDF引用的Magic Methods,它没有给出建议避免它们的理由.

经过一些谷歌搜索(并在这里结束一个不相关的问题),我想知道是否有人在这方面有任何推荐?

我在我的代码中使用了__get(),通常用于保存我并不总是使用的变量,例如

我可能有一个名为desc,category_id,time_added的表

我得到的东西会是这样的:

public function __get($name) {
    switch($name) {
        case 'name':
        case 'desc':
        case 'category':
        case 'time_added':
            $result = do_mysql_query();
            $this->name = $result['name'];
            $this->desc = $result['desc'];
            $this->category = $result['category'];
            $this->time_added = $result['time_added'];
            return $this->{$name};
        break;
        default:
            throw Exception("Attempted to access non existant or private property - ".$name);
    }
}

这似乎是一种很好的做事方式,因为我只需要从数据库中获取一些东西,如果需要的话,我可以为$ article-> time_added而不是摆弄数组.

这会被视为不良做法和服务器上的额外负载吗?

通常我会使用魔术方法扩展类,如果子类与get中的某些东西不匹配,则执行类似的操作.

public function __get($name) {
    switch($name) {
        case 'name':
        case 'desc':
        case 'category':
        case …

php performance magic-methods

10
推荐指数
1
解决办法
4853
查看次数

标签 统计

magic-methods ×1

performance ×1

php ×1