我在读这个页面 –
http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited
其中一个建议是避免使用魔术方法,引用Zend Performance PDF,它没有理由建议避免它们.
经过一些谷歌搜索(并在这里解决了一个无关的问题),我想知道有没有人在这方面有任何推论?
我在我的代码中使用__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这样的东西,而不是围绕数组.
这将被认为是坏的做法和额外的负载在服务器上?
通常,我将使用魔术方法扩展课程,如果子类不匹配某项内容,则可以执行此类操作.
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:
return parent::__get($name);
}
}
这是不好的做法和坏表现?扩展魔法的最大级别是3.
这是真的,他们是更慢…但差异是如此之小,速度vs代码是一个因素.对于更快的开发和维护的差异,值得我们担心吗?
有关统计资料,请参阅magic benchmarks