zoukankan      html  css  js  c++  java
  • php魔术方法__SET __GET

    __SET  设置一个不可访问的属性的时候 调用_set方法

    __GET 获取一个不可访问的属性的时候  调用_get 方法

    <?php
    
    class stu{
        private $a;
        private $b = 0;
        public $c;
        public $d = 0;
         
        //这里的 private 可以用 protected public 替代
        private function __get($name) {
            return 123;
        }
         
        //这里的 private 也可以用 protected public 替代
        private function __set($name, $value) {
            echo "This is set function";
        }
    }
     
    $s = new stu();
    echo "<pre>";
    var_dump($s->a);  //output: 123
    var_dump($s->b);  //output: 123
    var_dump($s->c);  //output: null
    var_dump($s->d);  //output: 0
    var_dump($s->e);  //output: 123
     
    $s->a = 3;   //output: This is set function
    echo "<br>";
    $s->c = 3;  //no output
    echo "<br>";
    $s->f = 3;  //output: This is set function

    看结果

  • 相关阅读:
    [译]git reflog
    [译]git rebase -i
    [译]git rebase
    [译]git commit --amend
    [译]git clean
    [译]git reset
    [译]git revert
    [译]git checkout
    [译]git log
    [译]git status
  • 原文地址:https://www.cnblogs.com/yhl664123701/p/5788663.html
Copyright © 2011-2022 走看看