zoukankan      html  css  js  c++  java
  • __set() And __get() 使用详解.

    正确姿势:

    final class private_{
        private $name='adminmas';
        private $sex ='20';
        public function __get($name){
            return $this->$name;
        }
        public function __set($name ,$value  ){
            $this->name = $value;
        }
    }
    $cl = new private_;
    $cl->name = 'jun';
    var_dump( $cl->name );

    错误姿势:

    final class private_{
        static private $name='adminmas';
        private $sex ='20';
        public function __get($name){
            return $this->$name;
        }
        public function __set($name ,$value  ){
            $this->name = $value;
        }
    }
    // $cl = new private_;
    private_::$name = 'jun';
    var_dump( private_::$name );

    总结:

    __get() And __set() 魔术函数无法获得静态方式访问的属性名

    必须得通过新建对象的方式访问.才能设置或获取和设置私有的属性.

    ps:

    PHP的高级特性:偶然间发现的.

    class na{
    
        static private $name= 'adminmas';
    
        //self::$name //正确
    
        public function __get($name){
            return self::$name; //正确
        }
    }
    $cl = new na;
    
    var_dump( $cl->name );
  • 相关阅读:
    多线程 -- H2O 生成、交替打印字符串
    打印零与奇偶数
    h2数据库的使用
    rtx应用和开发
    MongoDB--副本集
    Python 推导式
    Bootstrap组件
    Python logging日志的自动分割
    python watchdog监控文件修改
    Linux流量监控iftop
  • 原文地址:https://www.cnblogs.com/dsphper/p/4233299.html
Copyright © 2011-2022 走看看