在 php 中使用访问器方法与公共变量的权衡是什么

发布于 2024-09-19 02:34:16 字数 412 浏览 13 评论 0原文

我正在寻找有关在 php 中使用公共变量与私有变量和访问器方法的建议/经验。

例如 $obj->foo = 'a'; 回声 $obj->foo;

与 $obj->setFoo('a'); echo $obj->getFoo();

我喜欢公共变量的原因是它的语法更短——使用起来似乎更少的工作。我知道这可能会使以后的重构变得更加困难,但我从未经历过(这意味着,有时设计会发生变化 - 但通常需要更改访问器方法。)

另一个选择是将变量存储在数组中并使用魔术方法(__get/__set)来访问它们 - 然后我可以轻松使用公共变量,并能够重构或访问方法。

人们在 php 世界中所做的事情的任何经验或参考。

对于持有访问器方法的任何人来说,这是最好的方法,是否存在对公共变量的有效需求/使用?

I'm looking for advice/experience on using public variables vs private variables with accessor methods in php.

eg
$obj->foo = 'a';
echo $obj->foo;

vs
$obj->setFoo('a');
echo $obj->getFoo();

What I like about public variables is the shorter syntax - just seems less work to use. I understand that it could make refactoring more difficult later, but I've never experienced it (meaning, sometimes the design changes - but usually the accessor methods would need to be changed any.)

The other option is to store the variables in an array and use magic methods (__get/__set) to access them - then I have the ease of use of public variables with the ability to refactor or accessor methods.

Any experience or references of what people do in the php world.

And for anyone that hold the accessor method are the best way, is there a valid need/use for public variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-09-26 02:34:16

是的,访问器方法是一种开销,尽管使用 setter/getter 的语法与直接访问公共属性一样干净......稍微冗长一些,但同样干净。

访问器方法的最大好处是您可以使用 set 方法来验证值并拒绝不适当的值(例如,尝试将始终应该是字符串或数组的整数的属性设置为字符串或数组)...但此验证仅在外部情况下才有效代码无法直接访问该属性。

第二个好处是,如果您的代码具有相关属性,其中对属性 A 的更改也需要对属性 B 进行更改...如果属性是公共的,您无法控制(或强制)这一点。

使用 set 方法可以让您实现流畅的接口,或者使代码更简洁,例如:

echo $obj->setFoo('a');

而不是

$obj->setFoo('a'); echo $obj->getFoo();

在 set 方法中包含 return

Yes, accessor methods are an overhead, although the syntax for using setters/getters is just as clean as direct access of public properties... slightly more wordy, but just as clean.

Biggest benefit of accessor methods is that you can use your set method to validate values and reject inappropriate values (e.g. trying to set a property that should always be an integer to a string or an array)... but this verification only works if external code can't access the property directly.

Second benefit if your code has related properties, where a change to property A requires a change to property B as well... if the properties are public, you can't control (or enforce) this.

Using set methods allows you to implement fluent interface, or to cleaner code for instances like:

echo $obj->setFoo('a');

instead of

$obj->setFoo('a'); echo $obj->getFoo();

if you include a return in your set method

北斗星光 2024-09-26 02:34:16

setter 和 getter 始终是最佳选择,因为它们为验证/清理提供了更高的稳定性。

public function __call($name,$params = array())
{
    if(strstr('set',$name))
    {
    }//returns

    if(strstr('get',$name))
    {
    }//Returns

    if(strstr('run',$name))
    {
    }//Returns
}

setters and getters are always the best option because they provide more stability for validation / sanitization.

public function __call($name,$params = array())
{
    if(strstr('set',$name))
    {
    }//returns

    if(strstr('get',$name))
    {
    }//Returns

    if(strstr('run',$name))
    {
    }//Returns
}
ぽ尐不点ル 2024-09-26 02:34:16

Getters/Setters 出现在 Java 中。它们在脚本语言中没有地位,因此在 Python 和 Javascript 等语言中不受欢迎。它们使 API 变得更加麻烦。

正如您已经知道的,您可以使用 __get 来包装变量访问。这在脚本语言中是可取的。例如,它允许您在中央位置验证类型,而多个 setter/getter 中没有重复的代码。

 function __set($name, $value) {
     if (gettype($value) == $this->_types[$name]) {
          $this->_vars[$name] = $value;
     }
 }   // instead of twenty methods checking the type

实现空洞的 getter/setter 以获得看起来有企业精神的代码是一种代码味道。而且也不建议使用带有副作用的 getter/setter。始终使用真实的方法/消息来进行复杂的对象操作。

Getters/Setters arose in Java. They have no place in scripting languages, hence they are frowned upon in e.g. Python and Javascript. They make the API more combersome.

As you already know, you can use __get to wrap variable access. This is advisable in scripting languages. It allows you to e.g. validate types in a central location, you don't have duplicated code in multiple setters/getters.

 function __set($name, $value) {
     if (gettype($value) == $this->_types[$name]) {
          $this->_vars[$name] = $value;
     }
 }   // instead of twenty methods checking the type

It's a code smell to implement hollow getters/setters to have enterprisey-looking code. And it's also inadvisable to have getters/setters with side-effects. Always use real methods / messages for complex object operations.

且行且努力 2024-09-26 02:34:16

Setter 和 Getters 是封装的一种实现,这是 OOP 原则之一,它规定对象必须是一个黑盒子,只向其客户端暴露行为及其状态,但防止暴露其内部。这允许低耦合代码,因为您可以更改 Setter 和/或 Getter 内部的逻辑,而不会影响使用 API 的客户端代码。

正如许多其他人之前所说,还有其他实现,例如魔术方法。

Setters and Getters are one kind of implementation for encapsulation, one of the OOP principles, which says an object must be a black box just exposing behaviors and its state to its clients, but preventing the exposure of its internal. This allows low coupling code as you can change the logic inside a Setter and / or Getter without affecting the client code using your API.

As many other have stated before, there are other implementations such as magic methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文