PHP:封装的目的是什么? [英] PHP: What is the purpose of encapsulation?

查看:106
本文介绍了PHP:封装的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在专门讨论可以应用于属性和方法的公共,私有和受保护的关键字.我到处都看过,我知道它们的作用以及如何使用它们,但是看不出它们在编程时如何实用.有人可以解释或举一个例子吗?

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?

推荐答案

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

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;
}

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

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.

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

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.

这篇关于PHP:封装的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆