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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了