PHP Readonly Properties?

前端 未结 6 1205
闹比i
闹比i 2020-12-05 06:12

In using PHP\'s DOM classes (DOMNode, DOMEElement, etc) I have noticed that they possess truly readonly properties. For example, I can read the $nodeName property of a DOMNo

6条回答
  •  离开以前
    2020-12-05 06:48

    Class PropertyExample {
    
            private $m_value;
    
            public function Value() {
                $args = func_get_args();
                return $this->getSet($this->m_value, $args);
            }
    
            protected function _getSet(&$property, $args){
                switch (sizeOf($args)){
                    case 0:
                        return $property;
                    case 1:
                        $property = $args[0];
                        break;  
                    default:
                        $backtrace = debug_backtrace();
                        throw new Exception($backtrace[2]['function'] . ' accepts either 0 or 1 parameters');
                }
            }
    
    
    }
    

    This is how I deal with getting/setting my properties, if you want to make Value() readonly ... then you simply just have it do the following instead:

        return $this->m_value;
    

    Where as the function Value() right now would either get or set.

提交回复
热议问题