dongta1824 2014-11-12 14:11
浏览 51
已采纳

PHP堆栈类什么都不返回

I'm creating a small, multiclass system that extends each other. Lets say class a is a "core" and work as a checking/administration wrapper. class b is to check what $d is and call a method of class c, a user class, if it exists or trigger an error to class a back.

Here's my code:

<?php

class a {

    private $error;
    private $ok;

    public function __construct() {

        $this->error    = array();
        $this->ok       = array();

        // do other stuff here
    }
}

class b extends a {

    private $head;
    private $out;

    public function __construct() {

        parent::__construct();

        global $d;

        $this->head = "";
        $this->out  = "";

        if(method_exists($this,$d)) {
            $this->{$d}();
        } else
            $this->error[] = "method '$d' not found";
    }

    public function output() {
        return ($this->head==""?"":'<h1>'.$this->head.'</h1>').$this->out;
    }
}

class c extends b {

    private $me = "inside c";

    public function standard() {

        $this->head = "Heading";
        $this->out  = "it works!";

    }

}

$d      = "standard";
$c      = new c();

echo "<pre>".print_r($c,true)."</pre>";
echo $c->output();

?>    

if i ran $c->output() it returns nothing, but the print_r() returns this:

c Object
(
[me:c:private] => inside c
[head:b:private] => 
[out:b:private] => 
[error:a:private] => Array
    (
    )

[ok:a:private] => Array
    (
    )

[head] => Heading
[out] => it works!
)

Could anyone please help me with this?

  • 写回答

2条回答 默认 最新

  • duandanbeng1829 2014-11-12 14:27
    关注

    It's because you've declared all your class variables as private. This makes it so that only the class where they were declared can access them. Not even subclasses (derived classes) can see them.

    If you need subclasses to access the variables of a parent class, you should declare them as protected.

    http://php.net/manual/en/language.oop5.visibility.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块