最佳实践:PHP魔术方法__set和__get [英] Best practice: PHP Magic Methods __set and __get

查看:92
本文介绍了最佳实践:PHP魔术方法__set和__get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
魔术方法是PHP的最佳实践吗?

Possible Duplicate:
Are Magic Methods Best practice in PHP?

这些是简单的示例,但是假设您在类中拥有的属性多于两个.

These are simple examples, but imagine you have more properties than two in your class.

什么是最佳做法?

a)使用__get和__set

a) Using __get and __set

class MyClass {
    private $firstField;
    private $secondField;

    public function __get($property) {
            if (property_exists($this, $property)) {
                return $this->$property;
            }
    }

    public function __set($property, $value) {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
    }
}

$myClass = new MyClass();

$myClass->firstField = "This is a foo line";
$myClass->secondField = "This is a bar line";

echo $myClass->firstField;
echo $myClass->secondField;

/* Output:
    This is a foo line
    This is a bar line
 */

b)使用传统的setter和getters

b) Using traditional setters and getters

class MyClass {

    private $firstField;
    private $secondField;

    public function getFirstField() {
        return $this->firstField;
    }

    public function setFirstField($firstField) {
        $this->firstField = $firstField;
    }

    public function getSecondField() {
        return $this->secondField;
    }

    public function setSecondField($secondField) {
        $this->secondField = $secondField;
    }

}

$myClass = new MyClass();

$myClass->setFirstField("This is a foo line");
$myClass->setSecondField("This is a bar line");

echo $myClass->getFirstField();
echo $myClass->getSecondField();

/* Output:
    This is a foo line
    This is a bar line
 */

在本文中: http://blog.webspecies.co.uk/2011-05-23/the-new-era-of-php-frameworks.html

作者声称使用魔术方法不是一个好主意:

The author claims that using magic methods is not a good idea:

首先,在那时,使用PHP的魔术函数(__get,__ call等)非常流行.乍一看,它们没有什么问题,但实际上它们确实很危险.它们使API不清楚,无法自动完成,最重要的是它们很慢.他们的用例是入侵PHP以执行其不希望做的事情.而且有效.但不幸的事情发生了.

First of all, back then it was very popular to use PHP’s magic functions (__get, __call etc.). There is nothing wrong with them from a first look, but they are actually really dangerous. They make APIs unclear, auto-completion impossible and most importantly they are slow. The use case for them was to hack PHP to do things which it didn’t want to. And it worked. But made bad things happen.

但是我想听到更多关于此的意见.

But I would like to hear more opinions about this.

推荐答案

过去,您的情况完全一样.然后我寻求魔术方法.

I have been exactly in your case in the past. And I went for magic methods.

这是一个错误,问题的最后一部分说明了一切:

This was a mistake, the last part of your question says it all :

  • (比getter/setter慢)
  • 没有没有自动完成(这实际上是一个主要问题),并且由IDE进行了类型管理以进行重构和代码浏览(在Zend Studio/PhpStorm可以使用@property phpdoc注释进行处理,但这需要维护它们:很痛苦)
  • 文档(phpdoc)与应该如何使用您的代码不匹配,并且查看您的类也不会带来很多答案.这很令人困惑.
  • 在编辑后添加:具有属性的吸气剂与真实"方法更加一致,其中,getXXX()不仅返回私有属性,而且执行真实逻辑.您使用相同的命名.例如,您具有$user->getName()(返回私有属性)和$user->getToken($key)(已计算).在您的吸气剂获得的不仅仅是吸气剂并且需要做一些逻辑的那一天,一切仍然是一致的.
  • this is slower (than getters/setters)
  • there is no auto-completion (and this is a major problem actually), and type management by the IDE for refactoring and code-browsing (under Zend Studio/PhpStorm this can be handled with the @property phpdoc annotation but that requires to maintain them: quite a pain)
  • the documentation (phpdoc) doesn't match how your code is supposed to be used, and looking at your class doesn't bring much answers as well. This is confusing.
  • added after edit: having getters for properties is more consistent with "real" methods where getXXX() is not only returning a private property but doing real logic. You have the same naming. For example you have $user->getName() (returns private property) and $user->getToken($key) (computed). The day your getter gets more than a getter and needs to do some logic, everything is still consistent.

最后,这是IMO最大的问题:这很神奇.魔术非常非常糟糕,因为您必须知道魔术如何工作才能正确使用它.这是我在团队中遇到的一个问题:每个人都必须了解魔术,而不仅仅是您.

Finally, and this is the biggest problem IMO : this is magic. And magic is very very bad, because you have to know how the magic works to use it properly. That's a problem I've met in a team: everybody has to understand the magic, not just you.

写信人和二传手很痛苦(我讨厌他们),但他们值得.

Getters and setters are a pain to write (I hate them) but they are worth it.

这篇关于最佳实践:PHP魔术方法__set和__get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆