我应该为 PHP 中的 getter 和 setter 烦恼吗?
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我不想再次重复这样的论点:即使你现在不期望继承,将来可能仍然需要它,但它们仍然是使用 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:
查看 __get 和 __set 魔术方法,它将使您的代码保持干净,并且您不必因为不设置变量范围而感到懒惰。
显然您需要更多细节,但这将处理您的访问器。
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.
Obviously you'd have more detail, but that will handle your accessors.
你可能永远不知道你将来不需要延长课程。班级会做什么?它有多复杂?
封装是良好的 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.
如果我是你,我就不会打扰。你是对的,那种装饰只会让你的班级变得混乱。
I wouldn't bother if I were you. You're right, that decoration would just clutter up your class.
你可以覆盖 __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
我喜欢将类中的属性设为受保护或私有,然后使用 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.
如果是为了简历,他们很可能会像关心 getter/setter 这样的小细节一样(或更多)关心你的逻辑。
如果是我,我会在没有 getter/setter 的情况下将它们公开,但在它们上方添加注释,说一些类似“考虑将它们设为私有并根据应用程序的范围和首选代码样式使用 getter/setter”的内容这样
:
您的简历代码不会混乱
他们明白您不仅
懒惰并公开一切
无缘无故
盲目追随特定的
风格,你会考虑特定的
做出决定时的应用。
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:
Your resume code isn't cluttered
They understand that your not just
lazy and making everything public
for no reason
of blindly following a particular
style, you'd consider the particular
application in making a decision.
您想在片场或现场做一些特别的事情吗(或者只是其中之一是私人的)?
如果没有,那就不要在这些上浪费时间。
如果你不以“特殊方式”使用它们,你就不会获得任何好处,甚至可能会减慢一切
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
如果您不打算在设置或访问这些属性时执行特定行为,那么这并不重要。
如果稍后需要在访问或修改这些属性时具有特定行为,您始终可以重载 __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