我应该为 PHP 中的 getter 和 setter 烦恼吗?

发布于 2024-10-06 18:20:33 字数 188 浏览 5 评论 0原文

我正在用 PHP 创建一个新课程。我预计这门课不会再延长。我是否应该费心将类成员设置为私有并实现 getter 和 setter 函数?

我的一部分认为这只是浪费时间,而且只会增加我的代码量。

这堂课是为了简历。我用代码编写它是为了展示我的编码风格。问题是雇主是否愿意看到 getter 和 setter,还是只会让事情变得混乱?

I am making a new class in PHP. I don't anticipate this class ever being extended. Should I bother with making class members private and implementing getter and setter functions?

Part of me thinks that this is just a big waste of time and only serves to bulk up my code.

The class is for a resume. I am writing it in code to demonstrate my coding style. The question is will an employer want to see getters and setters, or will that just clutter things up?

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

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

发布评论

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

评论(9

木格 2024-10-13 18:20:33

我不想再次重复这样的论点:即使你现在不期望继承,将来可能仍然需要它,但它们仍然是使用 getter 和 setter 的更多理由:

  • getter 和 setter 会让你轻松地对您的属性进行验证
  • 您的代码将遵循良好的编码准则,因此其他人将更容易阅读。如果我遇到一个没有使用 getter 和 setter 的代码文件,我会对它非常怀疑,并且必须仔细阅读它很多次。
  • 此外,这不仅是每个班级的决定,也是整个项目的决定。您是否希望为您需要的某些类提供 getter 和 setter,而不为其他类提供 getter 和 setter?我相信一致性很重要。
  • 有一些工具可以为您自动生成编写 getter 和 setter。请记住,您只编写了一次代码,但您却阅读了很多次,

I don't want once again to repeat the argument that even that you don't expect inheritance now, you may still need it in the future, but they are still more reasons to use getters and setters:

  • Getters and setters will let you easily implement validation of your properties
  • Your code will follow good coding guidelines and as such it will be easier to be read by others. If I ran into a code file that didn't use getters and setters, I would be very skeptical about it and would have to read it carefully many times.
  • Also this is not only a per class decision, but a decision for your whole project. Do you want to have getters and setters for some classes you need them and not for some others? I believe that consistency is important.
  • There are tools that can autogenerate writing the getters and setters for you. Remember that you write the code only once, but you read it many times,
荭秂 2024-10-13 18:20:33

查看 __get 和 __set 魔术方法,它将使您的代码保持干净,并且您不必因为不设置变量范围而感到懒惰。

public function __get($name)
{
  return $this->$name;
}

public function __set($name, $value)
{
  $this->$name = $value;
}

显然您需要更多细节,但这将处理您的访问器。

Check out the __get and __set magic methods, it'll keep your code clean and you won't have to feel lazy for not setting variable scopel.

public function __get($name)
{
  return $this->$name;
}

public function __set($name, $value)
{
  $this->$name = $value;
}

Obviously you'd have more detail, but that will handle your accessors.

单挑你×的.吻 2024-10-13 18:20:33

你可能永远不知道你将来不需要延长课程。班级会做什么?它有多复杂?

封装是良好的 OO 设计的一部分。但良好的面向对象设计并不是编程的目标。目标是让事情正常运行并能够在需要时轻松修改它们。

You may never know that you will not need to extend the class in the future. What will the class be doing? How complex it is?

Encapsulation is part of good OO design. But good OO design is not the goal of programming. The goal is to have things working and be able to modify them easily when needed.

不语却知心 2024-10-13 18:20:33

如果我是你,我就不会打扰。你是对的,那种装饰只会让你的班级变得混乱。

I wouldn't bother if I were you. You're right, that decoration would just clutter up your class.

要走干脆点 2024-10-13 18:20:33

你可以覆盖 __get &据我所知__set。
http://php.net/manual/en/language.oop5.overloading.php

You can just override __get & __set as far as I understand.
http://php.net/manual/en/language.oop5.overloading.php

乙白 2024-10-13 18:20:33

我喜欢将类中的属性设为受保护或私有,然后使用 getter 和 setter。然后,我在设置器中使用代码来确保属性值在可接受的范围内。我喜欢这种方法,因为我发现它使调试更容易,因为我始终可以确定正在设置接受的值,或者正在抛出异常。它还让我更有信心,当我传递对象周围的属性中保存的值时,不会导致任何其他内容爆炸。

I like to make the properties in my class protected or private and then use getters and setters. I then use code in the setters to make sure that the property values are within accepted ranges. I like this approach as I find it makes debugging easier as I can always be sure that an accepted value is being set, or an exception is being thrown. It also gives me more confidence that when I pass objects around the values held in the properties are not going to cause anything else to blow up.

风流物 2024-10-13 18:20:33

如果是为了简历,他们很可能会像关心 getter/setter 这样的小细节一样(或更多)关心你的逻辑。

如果是我,我会在没有 getter/setter 的情况下将它们公开,但在它们上方添加注释,说一些类似“考虑将它们设为私有并根据应用程序的范围和首选代码样式使用 getter/setter”的内容这样

  1. 您的简历代码不会混乱

  2. 他们明白您不仅
    懒惰并公开一切
    无缘无故

  3. 表明,相反
    盲目追随特定的
    风格,你会考虑特定的
    做出决定时的应用。

If it's for a resume, it's likely that they care just as much (or more) about your logic as they do the little details like getters/setters.

If it was me, I'd make them public without getters/setters, but include a comment above them, saying something along the lines of "consider making these private and using getters/setters depending on the scope of the application and preferred code style of the team"

That way:

  1. Your resume code isn't cluttered

  2. They understand that your not just
    lazy and making everything public
    for no reason

  3. It shows that instead
    of blindly following a particular
    style, you'd consider the particular
    application in making a decision.
强辩 2024-10-13 18:20:33

您想在片场或现场做一些特别的事情吗(或者只是其中之一是私人的)?

如果没有,那就不要在这些上浪费时间。

如果你不以“特殊方式”使用它们,你就不会获得任何好处,甚至可能会减慢一切

Do you want to do someting special on set or get (or just one of them private)?

If not, then don't waste your time on these.

You don't gain anything on doing this if you don't use them in a "special way" and maybe it's even slowing everything down

旧街凉风 2024-10-13 18:20:33

如果您不打算在设置或访问这些属性时执行特定行为,那么这并不重要。

如果稍后需要在访问或修改这些属性时具有特定行为,您始终可以重载 __set 和 __get 方法。

请参阅:http://php.net/manual/en/language.oop5.overloading .php

If you don't plan to have specific behavior when setting or accesssing those property, it doesn't mater much.

If you are required to have specific behavior when accessing or modifying those property later you can always overload the __set and __get method.

See : http://php.net/manual/en/language.oop5.overloading.php

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