PHP - Zend 说避免魔术方法? [英] PHP - Zend say avoid Magic Methods?
问题描述
我正在阅读此页面 - http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited
其中一项建议是避免使用魔术方法,引用自 Zend Performance PDF,但没有理由建议避免使用它们.
经过一些 Google 搜索(并在此处结束了一个不相关的问题)后,我想知道是否有人在这方面有任何建议?
我在代码中经常使用 __get(),通常是为了保存我不总是使用的变量,例如
我可能有一个包含名称、desc、category_id、time_ added 的表
我的 get 看起来像这样:
<前>公共函数 __get($name) {开关($名称){案例名称":案例desc":案例类别":案例时间添加":$result = do_mysql_query();$this->name = $result['name'];$this->desc = $result['desc'];$this->category = $result['category'];$this->time_ added = $result['time_ added'];返回 $this->{$name};休息;默认:throw Exception("试图访问不存在的或私有的财产 - ".$name);}}这似乎是一种很好的做事方式,因为我只在需要时才从数据库中获取一些东西,而且我可以引用诸如 $article->time_ added 之类的东西,而不是摆弄数组.
这是否会被认为是不好的做法和服务器上的额外负载?
通常我会用魔法方法扩展类,如果子类与 get 中的某些内容不匹配,我会做这样的事情.
<前>公共函数 __get($name) {开关($名称){案例名称":案例desc":案例类别":案例时间添加":$result = do_mysql_query();$this->name = $result['name'];$this->desc = $result['desc'];$this->category = $result['category'];$this->time_ added = $result['time_ added'];返回 $this->{$name};休息;默认:return parent::__get($name);}}这会是不好的做法并且对性能不利吗?我在扩展魔法方法时的最大级别数是三个.
确实,它们更慢……但差异非常小,以至于速度与代码是一个因素.是否值得为更快的开发和维护而担心差异?
查看magic benchmarks了解统计数据
I was reading this page - http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited
And one of the recommendations was to avoid using Magic Methods, cited from a Zend Performance PDF which gives no reason for its recommendation to avoid them.
After some Google searching (and winding up here to an unrelated question) I wondered if anyone had any reccomendations on that front?
I use __get() alot in my code, usually to save variables that I don't always use e.g.
I may have a table with name, desc, category_id, time_added
My get would look something like this:
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); } }
This seems like a great way to do things as I only ever get something from the database if it's needed and I can refence things like $article->time_added rather than fiddling around with arrays.
Would this be considered bad practice and an extra load on the server?
Often I will extend classes with magic methods and do something like this if the child class doesn't match something in a get.
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); } }
Would this be bad practice and bad for performance? The maximum number of levels I have when extending magic methods is three.
It's true, they are slower... but the difference is so tiny that speed vs code is a factor. Is it worth worrying about the difference for quicker development and maintenance?
See magic benchmarks for stats
这篇关于PHP - Zend 说避免魔术方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!