【问题标题】:OO PHP - child class not inheriting parent methods and propertiesOO PHP - 子类不继承父方法和属性
【发布时间】:2016-11-12 12:30:23
【问题描述】:

我有一个非常简单的 OO 类结构,无法理解为什么子类没有从父类继承属性和方法。

这是我设置的基本示例:

//Main class:
class Main{

    //construct
    public function Main(){
        //get data from model
        $data = $model->getData();

        //Get the view
        $view = new View();

        //Init view
        $view->init( $data );

        //Get html
        $view->getHTML();
    }

}


//Parent View class
class View{

    public $data, $img_cache; 

    public function init( $data ){       
        $this->data = $data;
        $this->img_cache = new ImageCache();
    }

    public function getHTML(){

        //At this point all data is intact (data, img_cache)

        $view = new ChildView();

        //After getting reference to child class all data is null
        //I expected it to return a reference to the child class and be able to 
        //call the parent methods and properties using this object. 

        return $view->html();
    }

}


//Child View Class
class ChildView{

    public function html(){

        //I get a fatal error here: calling img_cache on a non-object.
        //But it should have inherited this from the parent class surely?

        return '<img src="'.$this->img_cache->thumb($this->data['img-src']).'"/>';       
    }
}

所以我希望子类继承父类的属性和方法。然后,当我获得对子类的引用时,它应该能够使用 img_cache 对象。但我在这里遇到一个致命错误:Call to a member function thumb() on a non-object

我哪里做错了?

【问题讨论】:

  • 您需要扩展子类以使其继承父类的属性。 php.net/manual/en/keyword.extends.php
  • class ChildView extends View
  • 您对 OOP 的理解有问题。不要在 View 类中创建对象 ChildView。相反,扩展 View 并在您的主控制器中调用 new ChildView();
  • @MP_Webby 请避免编辑您的原始问题。这样以后的读者就不会知道原始代码和答案有什么不同了。
  • @PEMapModder,是的,我明白这一点。这是一个错字,只是让我的问题更清楚。问题的主题没有改变。

标签: php oop inheritance


【解决方案1】:

您需要指定继承与扩展基类http://php.net/manual/en/keyword.extends.php

在您的孩子班上试试这个

//Child View Class
class ChildView extends View{

    public function html(){

        //I get a fatal error here: calling img_cache on a non-object.
        //But it should have inherited this from the parent class surely?

        return '<img src="'.$this->img_cache->thumb($this->data['img-src']).'"/>';       
    }
}

正如@ferdynator 所说,您正在实例化父级,而不是子级,因此您的Main 类也需要更改为实例化ChildView,而不是父级View

//Main class:
class Main{

    //construct
    public function Main(){
        //get data from model
        $data = $model->getData();

        //Get the view
        $view = new ChildView();

        //Init view
        $view->init( $data );

        //Get html
        $view->getHTML();
    }

}

【讨论】:

  • Main 中调用了View 类,而不是ChildView
  • 感谢@ferdynator。我忘了在我的示例中添加extend(已编辑),但仍然存在原始问题。
  • 是的,我明白了。对 OO / MVC 来说是新的,所以我仍然对它有所了解。答案标记为正确。
【解决方案2】:

您不会通过实例化来创建子类。你让它extends 超类。

然后您可以在抽象超类中创建一个抽象方法(或者如果您不希望它是抽象的,则可以创建一个默认实现),并在extends超类的子类中通过声明一个方法来实现它同名。

//Main class:
class Main{

    //construct
    public function Main(){
        //get data from model
        $data = $model->getData();

        //Get the view
        $view = new ChildView(); // <--- changed

        //Init view
        $view->init( $data );

        //Get html
        $view->getHTML();
    }

}


//Parent View class
abstract class View{ // <--- changed

    public $data, $img_cache; 

    public function init( $data ){       
        $this->data = $data;
        $this->img_cache = new ImageCache();
    }

    public abstract function getHTML(); // <--- changed
}


//Child View Class
class ChildView extends View{ // <--- changed

    public function getHTML(){ // <--- changed

        //I get a fatal error here: calling img_cache on a non-object.
        //But it should have inherited this from the parent class surely?

        return '<img src="'.$this->img_cache->thumb($this->data['img-src']).'"/>';       
    }
}

简单来说,子类和父类有相同的实例。它们只包含来自不同类的代码,但它们仍然是同一个对象。只有同一个对象才能获得相同的属性。

顺便说一句,构造函数使用__constructpublic function __construct 而不是public function Main。使用类名作为构造函数名称非常过时,可能会被弃用(不确定它是否已经被弃用)。

【讨论】:

  • 是的,我忘了添加extend,只是一个错字。它在我的代码中,但我仍然有继承问题。谢谢@PEMapModder
  • 我忘了注意我所做的重要更改之一。请看stackoverflow.com/posts/40562917/revisions
猜你喜欢
  • 2013-11-16
  • 1970-01-01
  • 2012-05-03
  • 2021-05-07
  • 2019-11-27
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多