PHP 受保护的类和属性,免受谁的侵害?
我刚刚开始使用 David Powers 的 PHP 面向对象解决方案 进行 OOP PHP,并且对 OOP 中的保护概念有点好奇。
作者清楚地解释了保护的工作原理,但关于不希望其他人能够更改属性的部分有点平淡。我很难想象一种情况,可以阻止其他人更改您的类,因为他们可以打开您的 class.php
并手动调整他们喜欢看到的 PHP 的任何内容始终为纯文本。
注意:以上所有内容都是由具有初学者编程理解的初学者编写的。
I'm just getting started with OOP PHP with PHP Object-Oriented Solutions by David Powers, and am a little curious about the notion of protection in OOP.
The author clearly explains how protection works, but the bit about not wanting others to be able to change properties falls a bit flat. I'm having a hard time imagining a situation where it is ever possible to prevent others from altering your classes, since they could just open up your class.php
and manually tweak whatever they pleased seeing as how PHP is always in plain text.
Caution: all of the above written by a beginner with a beginner's understanding of programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从你自己开始!
您可以使用不同的保护级别来指示您希望如何使用类。如果类成员是
protected
或private
,则只能由类本身访问。您不可能从“外部”代码(类外部的代码)中意外地搞砸该成员的值。假设您有一个只应该包含数字的类成员。您将其设置为受保护的,并添加一个设置器来检查其值只能是数字:
现在您可以确定,当您想要使用它。每当您想使用它时,您都可以跳过许多额外的错误检查代码。每当您尝试为其分配除数字之外的任何内容时,您都会收到一条非常响亮的错误消息,这使得很容易找到错误。
这是你为了减轻自己的工作负担而对自己施加的限制。因为程序员会犯错误。特别是像 PHP 这样的动态类型语言,会让你在没有注意到的情况下默默地犯下很多错误,这些错误后来会变成非常难以调试、非常严重的错误。
就其本质而言,软件非常软并且很容易退化为难以维护的鲁布·戈德堡逻辑机。 OOP、封装、可见性修饰符、类型提示等都是 PHP 为您提供的工具使您的代码“更难”,表达您希望代码的某些部分成为什么样子的意图,并使 PHP 能够为您强制执行此意图。
From yourself!
You use various levels of protection to indicate how you want a class to be used. If a class member is
protected
orprivate
, it can only be accessed by the class itself. There's no chance you can screw up the value of that member accidentally from "external" code (code outside the class).Say you have a class member that is only supposed to contain numbers. You make it
protected
and add a setter which checks that its value can only be numeric:Now you can be sure that
Foo::$num
will always contain a number when you want to work with it. You can skip a lot of extra error checking code whenever you want to use it. Any time you try to assign anything but a number to it, you'll get a very loud error message, which makes it very easy to find bugs.It's a restriction you put on yourself to ease your own work. Because programmers make mistakes. Especially dynamically typed languages like PHP let you silently make a lot of mistakes without you noticing, which turn into very hard to debug, very serious errors later on.
By its very nature, software is very soft and easily degrades into an unmaintainable Rube Goldberg logic machine. OOP, encapsulation, visibility modifiers, type hinting etc are tools PHP gives you to make your code "harder", to express your intent of what you want certain pieces of your code to be and enable PHP to enforce this intent for you.
受保护并不是真正防止任何人更改源代码,而只是一个 PHP OOP 中的类方法可见性
Protected is not really protecting from anyone to change the source code, but is just a class method visibility in PHP OOP
它们意味着它们以不同的方式受到保护...
没有什么可以阻止其他程序员打开类文件并更改访问修饰符。
隐藏数据是一件好事,因为暴露的越少,您可以控制的越多,可能引入的错误也就越少。
They mean they are protected in different ways...
Nothing stops another programmer from opening a class file and changing the access modifiers.
The hiding of data is a good thing because the less you expose, the more you can control and less bugs you can potentially introduce.