我应该还是不应该使用getter和setter方法? [英] Should I or should I not use getter and setter methods?

查看:113
本文介绍了我应该还是不应该使用getter和setter方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这确实让我感到烦恼,我开始认为一切都取决于个人选择,而不是一种提高效率或编写更好代码的特定方式:我应该还是不应该在其中使用getter/setter方法一个PHP项目?到目前为止,我已经读过的答案很矛盾,并且不完全适用于PHP(这不是一种编译语言).例如,在Stack Overflow("为什么使用getter和setters ? ).我为什么要在代码中使用它们,有很多充分的理由,但是所有子注释都提到了访问器的方式是邪恶的,应该避免,插在另外一些令人不安的注释之间,其中提到应该完全避免使用这些注释,因为这会弄乱您的代码".

Okay, this is really bugging me and I'm starting to think it all comes down to personal choice rather than a particular way of being more efficient or writing better code: Should I or should I not use getter/setter methods within a PHP project? The answers so far that I've read are rather conflicting and not entirely suited towards PHP, which is not a compiled language. For example, take this question on Stack Overflow ("Why use getters and setters?"). There's a number of good reasons presented as to why I should use them within my code, yet all the child comments mention how Accessors are evil and should be avoided, interspersed between a few more disturbing comments which mention that they should be avoided entirely because it "mucks up your code".

我得到的只是矛盾的答案,这些答案都与解释的PHP环境无关.有人可以阐明为什么/为什么不应该在PHP中使用它们以及该决定背后的原理吗?如果我们可以简单地将一个属性定义为私有或受保护的属性,真的吗?

All I'm getting is conflicting answers, none of which are relevant to a interpreted PHP environment. Can someone shine some light on why/why not they should be used within PHP, and their rationale behind that decision? Does it really matter if we can simply define a property as private or protected and, anyway:

封装的吸气剂和设置器的厚度很薄

The encapsulation getters and setters offer are laughably thin

...引自"sbi"(为什么使用getter和setter?)

... quoted from "sbi" (Why use getters and setters?)

我个人仍然不知道如何:

Personally, I still don't see how:

Class Foo {
    public $bar;

    function setBarType($val) {
       $this->bar = $val;
    }
}

$fee = new Foo();
$fee->setBarType(42);

优于此:

Class Foo {
    public $bar;
}

$fee = new Foo();
$fee->bar = 42;

推荐答案

因为如果更改设置值的实现(对于db),则不必更改调用方.另一个示例是,您可能需要在设置值之前对它进行一些检查.

Because if the implementation of how the value is set changes (to a db), you don't have to change the callers. Another example is that you may need to do some checking on the value before setting it.

拥有一个方法可以让您拦截该变量的设置/获取,在您似乎不需要它时执行此操作,从而使您的代码更易于更改.

Having a method lets you intercept the setting/getting of that variable, doing it when it doesn't seem like you need it makes your code more change friendly.

诸如C#和最新版本的JavaScript之类的语言允许您拦截属性的读写,因此您可以仅使用支持属性的语言来使用属性.

Languages like C# and recent versions of JavaScript allow you to intercept property reading and writing, so you can just use properties in languages that support it.

某些语言允许您拦截所有 JavaScript的Object.watch 或具有 PHP的__get .这允许您实现getter和setter,但由于它们为每个属性访问创建的开销而导致性能下降.此答案涉及吸气剂和吸气剂的其他问题. 最佳实践:PHP魔术方法__set和__get

Some languages allow you to intercept the reading/setting of all JavaScript's Object.watch, or inaccessible properties with PHP's __get. This allows you to implements getters and setters but you get a performance hit because of the overhead they create for every property access. This answer talks about other problems with getters and setters. Best practice: PHP Magic Methods __set and __get

仅制作样板吸气剂和吸气剂会更好,但几乎与公共财产一样糟糕.如果任何人都可以更改对象的状态(特别是具有多个属性),则将无法很好地对其进行封装. http://cspray.github.io/2012 /05/13/stop-calling-them-getters-setters.html

Just making boilerplate getters and setters is better, but is almost as bad as public properties. If anybody can change your object's state (specially with multiple properties), it won't be well encapsulated. http://cspray.github.io/2012/05/13/stop-calling-them-getters-setters.html

这篇关于我应该还是不应该使用getter和setter方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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