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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀