doushi3202 2015-09-02 11:12
浏览 362
已采纳

PHP:在类中访问私有变量

I've been doing a project in PHP for the last few hours and I have encountered into a problem.

The problem is I don't know how to access private variables in a class and I can't find it online.

Example:

<?php
    class Example{
        private $age;

        public function __construct() {
            $age = 14;
            $this->checkAge();
        }
        private function checkAge() {
            if($this->$age > 12)
                echo "welcome!";
        }
    }
    $boy = new Example();
?>

As far as I know, I should be able to access the variable with $this->$age but it isn't working.

Thank you.

EDIT: Got it working with help of the awesome stackoverflooooooooow community, this is how a working one looks.

<?php
    class Example{
        private $age;

        public function __construct() {
            $this->age = 14;
            $this->checkAge();
        }
        private function checkAge() {
            if($this->age > 12)
                echo "welcome!";
        }
    }
    $boy = new Example();
?>
  • 写回答

1条回答 默认 最新

  • dqly83915 2015-09-02 11:37
    关注

    Look at this approach.
    first: create Entity that stores and retrieves data inside of private $attributes array, and with magic __set(), __get() You can also do like: $object->variable = 123

    second: extend Entity with Human class and add some function specific to child class (for example hasValidAge()):

    <?php
        class Entity {
            private $attributes;
    
            public function __construct($attributes = []) {
                $this->setAttributes($attributes);
            }
    
            public function setAttribute($key, $value) {
                $this->attributes[$key] = $value;
                return $this;
            }
    
            public function setAttributes($attributes = []) {
                foreach($attributes AS $key => $value) {
                  $this->setAttribute($key, $value);
                }
            }
    
            public function getAttribute($key, $fallback = null) {
                return (isset($this->attributes[$key]))? 
                       $this->attributes[$key] : $fallback;
            }
    
            public function __get($key) {
                return $this->getAttribute($key);
            }
    
            public function __set($key, $value) {
                $this->setAttribute($key, $value);
            }
        }
    
        class Human extends Entity {
            public function __construct($attributes = []) {
                $this->setAttributes($attributes);
                $this->checkAge();
            }
    
            public function hasValidAge() {
                return ($this->getAttribute('age') > 12)? true : false;
            }
        }
    
        $boy = new Human(['name' => 'Mark', 'age' => 14]);
        if($boy->hasValidAge()) {
            echo "Welcome ".$boy->name."!";
        }
    
    ?>
    

    p.s. I've removed echo "Welcome!" part from constructor because it's not cool to do echo from model object, in our example Human is model of Entity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?