dttnb997315
dttnb997315
2012-09-21 02:59

使用父构造函数中指定的属性时出错

已采纳

When I call a method on a property instantiated in a parent class' constructor as follows:

class A
{
    function AA()
    {

    }
}

class C
{
    function __construct()
    {
        $this->A = new A();
    }
}

class B extends C
{
    function __construct()
    {
        $this->A->AA();
    }
}

I get this error:

Fatal error: Call to a member function AA() on a non-object in…

Why is that?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douhezhan5348 douhezhan5348 9年前

    Because constructors don't call parent constructors by default. You can trigger it with parent::__construct().

    点赞 评论 复制链接分享