Re: RFC: Locked Classes

From: Date: Tue, 12 Mar 2019 21:40:22 +0000
Subject: Re: RFC: Locked Classes
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-104684@lists.php.net to get a copy of this message
Hi! > While it can be useful, the ability to set an object property which is > not part of the class definition can also lead to subtle bugs. Banning > this for all objects would be a significant and painful breaking change, > so I propose instead the option to mark a particular class with a new > keyword, "locked". Isn't it just: trait Locked { public function __set($name, $value) { throw new RuntimeException("Property $name not declared!"); } public function __get($name) { throw new RuntimeException("Property $name not declared!"); } public function __unset($name) { throw new RuntimeException("Property $name not declared!"); } } The RFC says: While this can be achieved through strategic use of the __set, __get, and __unset magic methods, this is long-winded, hard to optimise, and interferes with other uses of those methods. I don't see how it's "long-winded" - it's just three one-liners, I don't see how "interferes with other uses" of these methods (if you want other uses, just add code to the method), and as for optimization, I think we already have optimization for pre-declared properties, and if you don't use any others, it would work just as well. -- Stas Malyshev smalyshev@gmail.com

« previous php.internals (#104684) next »