我是 OOP/PHP 新手。类的可见性和可扩展性的实用性是什么?

发布于 2024-09-03 05:17:39 字数 151 浏览 2 评论 0原文

我显然对这些概念是全新的。我只是不明白为什么你要限制对属性或方法的访问。看来你只要按照预期的结果编写代码即可。为什么要创建一个私有方法而不是简单地不调用该方法?是为了迭代对象创建(如果我说得正确的话),还是为了多个开发人员的情况(不要搞乱其他人的工作),或者只是为了不意外搞乱自己的工作?

I'm obviously brand new to these concepts. I just don't understand why you would limit access to properties or methods. It seems that you would just write the code according to intended results. Why would you create a private method instead of simply not calling that method? Is it for iterative object creation (if I'm stating that correctly), a multiple developer situation (don't mess up other people's work), or just so you don't mess up your own work accidentally?

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

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

发布评论

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

评论(8

青衫儰鉨ミ守葔 2024-09-10 05:17:39

你的最后两点非常准确 - 你不需要多个开发人员来搞乱你的东西。如果你在一个项目上工作足够长的时间,你会发现你已经忘记了很多一开始所做的事情。

隐藏某些内容的最重要原因之一是为了以后可以安全地更改它。如果某个字段是公共的,并且几个月后您想要更改它,那么每次该字段更改时都会发生其他情况,那么您就会遇到麻烦。因为它是公开的,所以无法知道或记住有多少其他地方直接访问了该字段。如果它是私有的,你就可以保证它不会在这个类之外被触及。您可能有一个公共方法围绕它,并且您可以轻松更改该方法的行为。

一般来说,公开的东西越多,你就越需要担心与其他代码的兼容性。

Your last two points are quite accurate - you don't need multiple developers to have your stuff messed with. If you work on a project long enough, you'll realize you've forgotten much of what you did at the beginning.

One of the most important reasons for hiding something is so that you can safely change it later. If a field is public, and several months later you want to change it so that every time the field changes, something else happens, you're in trouble. Because it was public, there's no way to know or remember how many other places accessed that field directly. If it's private, you have a guarantee that it isn't being touched outside of this class. You likely have a public method wrapped around it, and you can easily change the behavior of that method.

In general, more you things make public, the more you have to worry about compatibility with other code.

苏辞 2024-09-10 05:17:39

我们创建私有方法,以便我们的类的使用者不必关心实现细节 - 他们可以专注于我们的类为他们提供的一些漂亮的东西。

此外,我们有义务考虑公共方法的每一种可能的使用。通过将方法设为私有,我们减少了类必须支持的功能数量,并且可以更自由地更改它们。

假设您有一个 Queue 类 - 每次调用者向队列添加一个项目时,可能需要增加队列的容量。由于底层实现的原因,设置容量并非易事,因此您将其分解为一个单独的函数,以提高 Enqueue 函数的可读性。由于调用者不关心队列的容量(您正在为他们处理队列),因此您可以将该方法设为私有:调用者不会因多余的方法而分心,您不必担心调用者会做荒谬的事情容量,并且您可以随时更改实现,而不会破坏使用您的类的代码(只要它仍然在您的类定义的有限用例内设置容量)。

We create private methods so that consumers of our classes don't have to care about implementation details - they can focus on the few nifty things our classes provide for them.

Moreover, we're obligated to consider every possible use of public methods. By making methods private, we reduce the number of features a class has to support, and we have more freedom to change them.

Say you have a Queue class - every time a caller adds an item to the queue, it may be necessary to to increase the queue's capacity. Because of the underlying implementation, setting the capacity isn't trivial, so you break it out into a separate function to improve the readability of your Enqueue function. Since callers don't care about a queue's capacity (you're handling it for them), you can make the method private: callers don't get distracted by superfluous methods, you don't have to worry that callers will do ridiculous things to the capacity, and you can change the implementation any time you like without breaking code that uses your class (as long as it still sets the capacity within the limited use cases defined by your class).

阳光的暖冬 2024-09-10 05:17:39

这一切都归结为封装。这意味着隐藏类的内部并只关心它的作用。如果您想要一个信用卡处理类,您并不真正关心它“如何”处理信用卡。您只希望能够执行: $creditCardProcessor->charge(10.99, $creditCardNumber); 并期望它能够正常工作。

通过将某些方法设为公开,将其他方法设为私有或受保护,我们为其他方法留下了入口,以便他们知道从哪里调用代码是安全的。公共方法和变量称为“接口”。

对于任何类,您都有一个实现。这就是班级履行职责的方式。如果是冰沙制作类,类如何添加配料、添加什么配料等都是实现的一部分。外部代码不应该知道和/或关心实现。

类的另一端是它的接口。接口是类的开发人员打算由外部代码调用的公共方法。这意味着您应该能够调用任何公共方法并且它将正常工作。

It all comes down to encapsulation. This means hiding the insides of the class and just caring about what it does. If you want to have a credit card processing class, you don't really care 'how' it processes the credit card. You just want to be able to go: $creditCardProcessor->charge(10.99, $creditCardNumber); and expect it to work.

By making some methods public and others private or protected, we leave an entry way for others so they know where it is safe to call code from. The public methods and variables are called an 'interface'.

For any class, you have an implementation. This is how the class carries out its duty. If it is a smoothie making class, how the class adds the ingredients, what ingredients it adds, etc are all part of the implementation. The outside code shouldn't know and/or care about the implementation.

The other side of the class it its interface. The interface is the public methods that the developer of the class intended to be called by outside code. This means that you should be able to call any public method and it will work properly.

怂人 2024-09-10 05:17:39

使用封装有多种原因,其中最重要的原因之一是:想象一下使用由其他人编写的大型、复杂的库。如果每个对象都不受保护,您可能会在不知不觉中访问或更改开发人员从未打算以这种方式操纵的值。

隐藏数据使程序更容易概念化并且更容易实现。

There are several reasons for using encapsulation, one of the strongest is: Imagine using a large, complicated library written by someone else. If every object was unprotected you could unknowingly be accessing or changing values that the developer never intended to be manipulated in that way.

Hiding data makes the program easier to conceptualize and easier to implement.

要走干脆点 2024-09-10 05:17:39

这都是关于封装的。方法是私有的,它们执行内部繁重的工作,同时公开使事情变得简单的优雅函数。例如,您可能有一个 $product->insert() 函数,它利用 4 个内部函数来验证单例数据库对象、使查询安全等 - 这些是不需要公开的内部函数,如果被调用,可能会搞乱您(开发人员)已经制定的其他结构或流程。

It's all about encapsulation. Methods are private that do the inner grunt work while exposing graceful functions that make things easy. E.g. you might have an $product->insert() function that utilizes 4 inner functions to validate a singleton db object, make the query safe, etc - those are inner functions that don't need to be exposed and if called, might mess up other structures or flows you, the developer, have put in place.

旧竹 2024-09-10 05:17:39

多个开发人员的情况(不要
搞乱别人的工作),或者只是
这样你就不会搞砸你自己的工作
不小心?

主要是这两点。将方法公开表示“这就是客户应该如何使用该类”,将其设为私有表示“这是一个实现细节,可能会在没有警告的情况下更改,客户不应该关心”,并强制客户遵循该方法建议。

具有一些记录良好的公共方法的类比所有内容都是公开的类更容易被不熟悉它的人(很可能是它的原作者,6 个月内第一次看到它)使用,包括所有您不关心的小实施细节。

a multiple developer situation (don't
mess up other people's work), or just
so you don't mess up your own work
accidentally?

Mainly these two things. Making a method public says "this is how the class is supposed to be used by its clients", making it private says "this is an implementation detail that may change without warning and which clients should not care about" AND forces clients to follow that advice.

A class with a few, well documented public methods is much easier to use by someone who's not familiar with it (which may well be its original author, looking at it for the first time in 6 months) than one where everything is public, including all the little implementation details that you don't care about.

葬花如无物 2024-09-10 05:17:39

它使协作变得更容易,您告诉类的用户哪些部分不应该经常更改,并且如果他们仅使用公共方法,您可以保证您的对象将处于有意义的状态。

它不需要像区分私有/公共/任何东西那样严格(我的意思是由语言强制执行)。例如,在 Python 中,这是通过命名约定来完成的。您知道您不应该弄乱任何标记为非公开的内容。

It makes collaboration easier, you tell the users of your classes what parts should not change so often and you can guarantee that your object will be in a meaningful state if they use only public methods.

It does not need to be so strict as distinguishing between private/public/whatever (I mean enforced by the language). For example, in Python, this is accomplished by a naming convention. You know you shouldn't mess with anything marked as not public.

山田美奈子 2024-09-10 05:17:39

例如 - 私有/受保护的方法可能是在另一个(公共)方法中调用的某个类的一部分。如果在更公共的方法中调用该部分,那就有意义了。然而您不希望在其他地方调用这些方法。

这与类属性完全相同。是的,你可以编写全公共类,但这有什么乐趣呢?

For example - private/protected method may be part of some class which is called in another (public) method. If that part is called in more public methods, it makes sense. And yet you don't want these methods to be called anywhere else.

It's quite the same with class properties. Yes, you can write all-public classes, but whats the fun in that?

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