【发布时间】:2018-06-29 08:59:20
【问题描述】:
在这里考虑这段代码:
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 可见性,为什么还要为它烦恼?
有没有办法阻止我错过的这种事情?
【问题讨论】:
-
可见性也传达了意图。
标签: php class oop reflection closures