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 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页