开发者

Issue With Objects In PHP

I have a problem with objects in PHP.

http://codepad.org/HdqCVJlw

<?php
error_reporting( E_ALL | E_STRICT );
##
class Loader {
    function view() {
        echo 'I did it! )';
    }
}
class Controller {
    public $loader;
开发者_运维问答    function __construct() {
        $this -> loader = new Loader;
    }
}

##
class Foo extends Controller {
    function index() {
        $this -> loader -> view();
    }
}

There are three classes: Loader, Controller and Foo. I want to use method of class Loader in Foo class. I know that __construct() wouldn't run because Foo extends from Controller, but anyway I don't get any error.

So how to use that method there with condition that in Foo I won't need to write more code?

P.S. I'm creating MVC...


I know that __construct() wouldn't run because Foo extends from Controller, but anyway I don't get any error.

When instantiating Foo, Controller's __construct() will be run.

The exception is if you make a __construct() within Foo, and forget to call parent::__construct().

The code you posted works fine.


Add a constructor to Foo that calls parent::__construct();.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜