首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类层次结构

类层次结构
EN

Stack Overflow用户
提问于 2012-06-09 01:25:27
回答 1查看 119关注 0票数 0

我有下面的代码,我希望返回“已工作”,但什么也不返回。

代码语言:javascript
复制
class Foo {
    public function __construct() {
        echo('Foo::__construct()<br />');
    }

    public function start() {
        echo('Foo::start()<br />');

        $this->bar = new Bar();
        $this->anotherBar = new AnotherBar();
    }
}

class Bar extends Foo {
    public function test() {
        echo('Bar::test()<br />');

        return 'WORKED';
    }
}

class AnotherBar extends Foo {
    public function __construct() {
        echo('AnotherBar::__construct()<br />');

        echo($this->bar->test());
    }
}

$foo = new Foo();
$foo->start();

路由器

代码语言:javascript
复制
Foo::__construct() <- From $foo = new Foo();
Foo::start() <- From Foo::__construct();
Foo::__construct() <- From $this->bar = new Bar();
AnotherBar::__construct() <- From $this->anotherBar = new AnotherBar();

因为我从$bar类定义了Foo,并且将AnotherBar扩展到Foo,所以我希望从Foo中获得已经定义的变量。

我看不出是怎么回事。我从哪里开始?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-09 01:27:44

AnotherBar实例从未调用过它的start方法,因此它的$this->bar是未定义的。

在显示错误时,您将得到以下消息:

注意:未定义的属性: AnotherBar::$bar in - on第20行,致命错误:调用非对象在第20行中的成员函数test()

您可以在<?php行之后包含以下代码以查看所有错误:

代码语言:javascript
复制
ini_set('display_errors', 'on');
error_reporting(E_ALL);

当然,您也可以通过php.ini实现这一点,这将是一个更干净的解决方案。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10957682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档