PHP保护的类和属性,不受谁保护? [英] PHP protected classes and properties, protected from whom?

查看:60
本文介绍了PHP保护的类和属性,不受谁保护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用David Powers的 PHP面向对象解决方案来开始使用OOP PHP,并且对OOP中的保护概念有些好奇.

作者清楚地说明了保护的工作原理,但是关于不希望其他人能够更改属性的说法有些平淡.我很难想象这样一种情况,即有可能阻止其他人更改您的类,因为他们可以打开您的class.php并手动调整他们喜欢的PHP文本格式. /p>

警告:以上所有内容都是由初学者编写的,具有初学者对编程的理解.

解决方案

来自您自己!

您使用各种级别的保护来指示您希望如何使用一个类.如果类成员是protectedprivate,则只能由类本身访问.您没有机会意外地从外部"代码(类外部的代码)中破坏该成员的价值.

假设您有一个仅包含数字的类成员.将其设置为protected并添加一个setter,以检查其值只能为数字:

class Foo {

    protected $num = 0;

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

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

这是您为简化自己的工作而施加的限制.因为程序员会犯错误.尤其是动态类型化的语言(如PHP)使您可以不知不觉地悄悄地犯了很多错误,这些错误使调试变得非常困难,后来又引起非常严重的错误.

从本质上讲,软件非常软,并且很容易降级为不可维护的Rube Goldberg逻辑机. OOP,封装,可见性修改器,类型提示等都是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.

解决方案

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.

这篇关于PHP保护的类和属性,不受谁保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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