zoukankan      html  css  js  c++  java
  • PHP类动态属性问题

    动态属性问题给程序带来很多麻烦,以下是一位朋友的解决方法如下:

    From : http://bbs.php100.com/read-htm-tid-25526.html

    <?php
    
    class testClass {
    
        private $va;
        private $vb;
        private $vc;
        private $allVars;
    
        public function __construct(){
            echo 'testClass __construct'.'</br>';
            //$this->va = 1;
            $this->getAllVars();
        }
    
        public function __GET($name) {
            return $this->$name;
        }
        public function __SET($name, $value) {
            foreach($this->allVars as $key) {
                if ($name == "$key") {
                    $this->$name = $value;
                    return;
                }
            }
            throw new exception ('你访问了未定义的属性');
        }
        private function getAllProps() {
            $r = new ReflectionClass($this);
            return ($r->getProperties());
    
        }
        private function getAllVars() {
            $classProps = $this->getAllProps();
            foreach($classProps as $key)
            {
                $temparr =    get_object_vars($key);
                $this->allVars[] .= $temparr['name'];
            }
        }
    }
    
    $rc = new testClass();
    $rc->va = 5;
    $rc->vb = 5;
    $rc->vc = 5;
    $rc->vd = 6;
    ?>
  • 相关阅读:
    ELK 一些截图
    AD域
    NPOI
    搭建harbor
    【【【【日常问题记录】】】】
    golang yaml配置文件解析
    golang操作mysql使用总结
    【转】mysql优化步骤
    【转】Mysql事务,并发问题,锁机制
    golang curl
  • 原文地址:https://www.cnblogs.com/Athrun/p/php_class_property.html
Copyright © 2011-2022 走看看