Is there a way to disable adding properties into a class from an instance of the class.
What I mean is this:
Consider this class:
class a {
private $v1;
public $v2;
function func(){
...
}
}
If I do this:
$ins = new a;
$ins->temp = "A variable created from outside the class! C*ap!";
var_dump($ins);
The output:
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?`
Perhaps you can implement __set() and throw an exception from there:
class a {
private $v1;
public $v2;
public function __set($name, $value) {
throw new Exception("Cannot add new property \$$name to instance of " . __CLASS__);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With