Is there a way to disable adding properties into a class from an instance of the class?

后端 未结 1 1518
遥遥无期
遥遥无期 2020-12-06 05:47

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 {
 pri         


        
相关标签:
1条回答
  • 2020-12-06 06:24

    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__);
        }
    }
    
    0 讨论(0)
提交回复
热议问题