Hide specific class fields from print_r or var_dump

前端 未结 6 1585
无人及你
无人及你 2021-01-02 01:22

Is it possible to hide a specific class fields from print_r ?



        
6条回答
  •  执笔经年
    2021-01-02 01:24

    I built upon Meir's solution to extend it to work with multiple objects of the class. Here is my solution:

    hiddenVarKey) {
               $this->hiddenVarKey = sha1(rand());
            }
            if(!empty($set_to_new_val)){
               self::$hiddenVarMap[$this->hiddenVarKey] = $set_to_new_val;
            }
    
            return self::$hiddenVarMap[$this->hiddenVarKey];
        }
    }
    
    $test = new myClass;
    
    $test->visibleVar = range(1,5);
    print_r($test);
    print_r($test->visibleVar);
    
    
    $test->hiddenVar(range(100,105));
    print_r($test);
    print_r($test->hiddenVar());
    
    ?>
    

提交回复
热议问题