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

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题