PHP 受保护的类和属性,免受谁的侵害?

发布于 2024-10-11 04:04:11 字数 254 浏览 4 评论 0原文

我刚刚开始使用 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 技术交流群。

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

发布评论

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

评论(3

无需解释 2024-10-18 04:04:11

从你自己开始!

您可以使用不同的保护级别来指示您希望如何使用类。如果类成员是protectedprivate,则只能由类本身访问。您不可能从“外部”代码(类外部的代码)中意外地搞砸该成员的值。

假设您有一个只应该包含数字的类成员。您将其设置为受保护的,并添加一个设置器来检查其值只能是数字:

class Foo {

    protected $num = 0;

    public function setNum($num) {
        if (!is_int($num)) {
            throw new Exception('Not a number!!!');
        }
        $this->num = $num;
    }
}

现在您可以确定,当您想要使用它。每当您想使用它时,您都可以跳过许多额外的错误检查代码。每当您尝试为其分配除数字之外的任何内容时,您都会收到一条非常响亮的错误消息,这使得很容易找到错误。

这是你为了减轻自己的工作负担而对自己施加的限制。因为程序员会犯错误。特别是像 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 or private, 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:

class Foo {

    protected $num = 0;

    public function setNum($num) {
        if (!is_int($num)) {
            throw new Exception('Not a number!!!');
        }
        $this->num = $num;
    }
}

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.

茶花眉 2024-10-18 04:04:11

受保护并不是真正防止任何人更改源代码,而只是一个 PHP OOP 中的类方法可见性

声明为 public 的类成员可以在任何地方访问。声明为 protected 的成员只能在类本身内部以及继承类和父类中访问。声明为私有的成员只能由定义该成员的类访问。

Protected is not really protecting from anyone to change the source code, but is just a class method visibility in PHP OOP

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

鸩远一方 2024-10-18 04:04:11

它们意味着它们以不同的方式受到保护...

  • 私有变量除了在类内部之外对任何地方都是不可见的。
  • 受保护的变量对实例化对象不可见,但对从该类继承的类以及类本身可见。

没有什么可以阻止其他程序员打开类文件并更改访问修饰符。

隐藏数据是一件好事,因为暴露的越少,您可以控制的越多,可能引入的错误也就越少。

They mean they are protected in different ways...

  • Private variables are not visible to anywhere except from within the class.
  • Protected variables are not visible to the instantiated object, but are visible to classes which inherit from that class, as well as the class itself.

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.

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