当闭包和反射可用时,PHP中OOP可见性的意义是什么? [英] What is the point of OOP visibility in PHP when Closures and Reflections are available?

查看:106
本文介绍了当闭包和反射可用时,PHP中OOP可见性的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处考虑此代码:

final class TinkerWithMe {
    protected $key1 = 19;
    private $key2 = 88;
}

$class = new TinkerWithMe();

$getKeys = function() {
    return array($this->key1, $this->key2);
};

$oldKeys = $getKeys->call($class);

$newKey1 = 96;
$newKey2 = 42;

$setKeys = function() use ($newKey1, $newKey2) {
    $this->key1 = $newKey1;
    $this->key2 = $newKey2;
};

$setKeys->call($class);

为什么当使用闭包"或反射"轻松地绕过OOP可见性时?

Why bother with OOP visibility when you can get around it so easily with Closures or Reflections?

有没有办法阻止我想念的这种东西?

Is there a way of blocking this kind of thing that I am missing?

推荐答案

可见性修饰符不是保护性的元素,也不是确保任何类型的安全性或任何种类的安全性.它们是标记,用于指示打算使用一段代码的方式.

Visibility modifiers aren't iron clad protection or ensure any sort of security or anything of that sort. They're markers for how a piece of code is intended to be used.

在编写类似于类的代码时,其他代码将与之耦合;这意味着您将编写其他代码来调用该类的方法或访问该类的属性.为了最大程度地减少与绝对必要的耦合,您将希望使该类的 public接口尽可能小.当您将某些内容指定为public时,就是将其标记为通用".这样,该公共部分应该相当稳定并且不能更改,否则如果您更改了公共代码,就有冒着破坏大量耦合代码的风险.

When writing a piece of code like a class, other pieces of code are going to couple with it; meaning you will write other code that calls methods of that class or accesses properties of that class. In order to minimise coupling to the absolute necessary, you will want to keep the public interface of the class as small as possible. When you designate something as public, you're marking it "for general use". That public piece should then be rather stable and not change, or you risk breaking a lot of coupled code if you do change it.

将某物标记为protectedprivate会将这些零件标记为不用于一般消费;它阐明这些实现细节将来可能会更改,或者不应被未知"其他代码使用.这样一来,您可以在以后根据需要更轻松地重构这些部分,并更好地了解可能会破坏哪些其他部分.当外部代码在不完全了解应该如何修改内部值的情况下直接修改内部值时,它还有助于确保类的内部状态是一致的.

Marking something as protected or private marks these parts as not for general consumption; it clarifies that these implementation details might change in the future or are not supposed to be used by other code "not in the know". This allows you to easier refactor those parts later as necessary and having a better idea of what other parts might break as a result. It also helps to ensure the internal state of the class is consistent when external code won't directly modify internal values without completely understanding how it should do so.

PHP会在天真地尝试从外部访问protectedprivate属性时抛出错误,从而在一般情况下帮助您兑现这些标记.可以防止大多数意外使用和不必要的耦合. PHP不会向后弯腰以确保在所有可能的情况下都无法访问那些属性.如果您真的想射击自己的脚,那就去吧.也许您出于测试目的需要这样做;绝对阻止您这样做会适得其反.

PHP will help you honour those markers in the general case by throwing errors when naïvely trying to access protected or private properties "from the outside"; that prevents most accidental usage and unwanted coupling. PHP is not going to bend over backwards to ensure those properties stay inaccessible under all possible circumstances. If you're really bent on shooting your own foot, so be it. Perhaps you need to do so for testing purposes; it would be counter productive to absolutely prevent you from doing so.

Python口语中:

我们都是成年人.

We're all consenting adults here.

这篇关于当闭包和反射可用时,PHP中OOP可见性的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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