PHP:封装的目的是什么?

发布于 2024-09-17 21:32:13 字数 111 浏览 3 评论 0原文

我正在具体谈论可应用于属性和方法的 public、private 和 protected 关键字。我到处都看过,我知道它们的作用以及如何使用它们,但不知道它们在编程时如何实用。有人可以解释一下或者举个例子吗?

I'm talking specifically about the public, private and protected keywords that can apply to properties and methods. I've looked everywhere and I know what they do and how to use them, but don't see how they would be practical when programming. Could somebody explain or give an example?

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

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

发布评论

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

评论(4

夜司空 2024-09-24 21:32:13

封装(范围)的主要目的是确保您编写的代码不会被破坏。这通常适用于范围,所以让我使用函数内局部变量的一个更简单的示例:

function xyz ($x) {
  $y = 1;
  while ($y <= 10) {
    $array[] = $y * $x;
    $y++;
  }
  return $array;
}

该函数的目的是传递一个数字并返回一个数组。示例代码非常基本。为了使函数 xyz() 可靠,您需要保证它每次都执行完全相同的操作。那么,如果有人能够从外部更改 $y 或 $array 的初始值呢?甚至$x?如果您能够从函数外部执行此操作,则无法再保证该函数返回什么。

这就是范围(封装)发挥作用的地方。它是一种设置边界的方法,分配变量(以及函数、属性、方法、对象)可以做什么和不能做什么的权限,以确保该段代码始终准确地执行预期的操作。

以任何内置 php 函数为例,例如...strtolower() 或 preg_match() 或...以及任何东西。他们期望参数被传递给他们,并且他们返回一些特定的东西。内部有变量、循环等......来获取输入并吐出输出。如果您能够从外部更改这些内容,则无法保证 strotolower() 会返回您提供给它的小写字符串,这违背了可重用代码块的目的。

当您自己编程时,这不一定很有用,但是当您编写供许多人使用和共享的代码时,尤其是使用涉及附加组件/插件等的代码时……这对于确保您的代码执行其应该执行的操作,并以预期的方式访问/使用/调用。

The primary purpose of encapsulation (scope) is to ensure that you write code that can't be broken. This applies to scope in general, so let me use a simpler example of a local variable inside a function:

function xyz ($x) {
  $y = 1;
  while ($y <= 10) {
    $array[] = $y * $x;
    $y++;
  }
  return $array;
}

The purpose of this function is to pass a number and return an array. The example code is pretty basic. In order for function xyz() to be dependable, you need to be guaranteed that it does the exact same thing every time. So what if someone had the ability to from the outside change that initial value of $y or $array? Or even $x? If you were able to do that from outside of the function, you could no longer guarantee what that function is returning.

That is where scope (encapsulation) comes into play. It is a way of setting boundaries, of assigning permissions of what can and can't be done with your variables (and functions, properties, methods, objects) to make sure that bit of code will always do exactly what it is expected to do.

Take for instance any built-in php function like...strtolower() or preg_match() or ...well anything. They expect arguments to be passed to them, and they return something specific. Internally there are variables, loops, etc... to take the input and spit out an output. If you were to be able to change that stuff from the outside, there would be no way to guarantee that strotolower() will return a lowercased string you feed it, and that defeats the purpose of having a reusable code block.

This isn't necessarily all that useful when you are programming for yourself, but when you are writing code to be used and shared by many people, especially with using code that involves addons/plugins, etc... it is invaluable for ensuring your code does what it is supposed to be doing, and is accessed/used/called in an expected way.

可爱暴击 2024-09-24 21:32:13

私有/受保护方法用于 C++ 和 Java 等编译语言。如果代码被编译而不是以脚本形式保留,则这些规则是可执行的。在这些语言中,尤其可以隐藏繁琐的内部或机器级实现。

PHP 只有这些功能,因为它的开发人员认为,当人们嘲笑 PHP4 不是一种适当的面向对象语言时,就意味着缺乏受保护/私有方法。
在 PHP 中使用封装作为设计概念是毫无争议的。然而,这些语言结构的使用并不是必需的,并且无论如何都可以轻松地在源代码中删除。但是,如果没有适当的库文档,或者如果共同程序员不尊重编码风格,则限制访问会很有用。

因此,总而言之:技术用例很少,鼓励使用指定的接口来抽象流程。然而,通常更有意义的方式是设计 API,使所有所需的功能和数据都易于使用,从而减少访问无关内部的诱惑。暴露的越多,以后改变内部处理就越困难。您公开的固有数据越多,出现的解决方法或复杂的对象结构就越少。

Private/protected methods are used in compiled languages like C++ and Java. If code is compiled instead left in script form these rules are enforceable. And in these languages particularily can hide cumbersome internal or machine level implementations.

PHP only has these functions, because its developers thought the lack of protected/private methods was meant when people were deriding PHP4 as not being a proper object oriented language.
The use of encapsulation as design concept in PHP is undisputed. The use of these language constructs is however not necessary, and can easily be removed in source code anyway. But in lieu of proper library documentation or if coprogrammers don't honor coding style, it's useful to restrict access.

So, in conclusion: technical use case meager, encouraging to use designated interfaces to abstract processes okay. Often however it is more senseful to design APIs in a way that makes all desirable functionality and data easily available, so the temption to access extraneous interna is reduced. The more you expose, the more difficult it gets to change internal processing later. The more inherent data your expose, the less workarounds or convoluted object structures arise.

烂柯人 2024-09-24 21:32:13

我认为 Grady Booch 总结得很好:

“封装用于分离抽象的契约接口及其实现”

I think Grady Booch sums it up quite nicely:

"encapsulation serves to separate the contractual interface of an abstraction and its implementation"

沫尐诺 2024-09-24 21:32:13

您可以在此处找到有关相同内容的精彩讨论。尽管它与 .net 有关(与 php 无关),但讨论仍然相当笼统,其中还包括对维基百科文章的讨论。

you can find a nice discussion on the same here. Although it is related to .net (not on php) but still a discussion is quite general which includes discussion on the wikipedia article also.

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