douxun4860 2016-11-12 12:30
浏览 161
已采纳

OO PHP - 子类不继承父方法和属性

I have a very simple OO class structure and cannot get my head around why the child class is not inheriting the properties and methods from the parent.

This is a basic example of my set up:

//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']).'"/>';       
    }
}

So I expected the child class to inherit the properties and methods from the parent. Then when I get a reference to the child class it should be able to use the img_cache object. But I get a fatal error here: Call to a member function thumb() on a non-object.

Where have I gone wrong with this?

  • 写回答

2条回答 默认 最新

  • dongliufa6380 2016-11-12 12:32
    关注

    You need to specify the inheritance with extends the base class http://php.net/manual/en/keyword.extends.php

    Try this for your child class

    //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']).'"/>';       
        }
    }
    

    Also as @ferdynator says, you are instantiating the parent, not the child, so your Main class also needs to be changed to instantiate ChildView, not the parent 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();
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用