Thi*_*key 12 php oop properties
有没有办法禁止从类的实例向类中添加属性.
我的意思是:
考虑这个课程:
class a {
private $v1;
public $v2;
function func(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
$ins = new a;
$ins->temp = "A variable created from outside the class! C*ap!";
var_dump($ins);
Run Code Online (Sandbox Code Playgroud)
输出:
object(a)#1 (3) { ["v1":"a":private]=> NULL ["v2"]=> NULL ["temp"]=> string(48) "A variable created from outside the class! C*ap!" }
Can this be disabled?
`
Bol*_*ock 20
也许您可以__set()
从那里实现并抛出异常:
class a {
private $v1;
public $v2;
public function __set($name, $value) {
throw new Exception("Cannot add new property \$$name to instance of " . __CLASS__);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1995 次 |
最近记录: |