PHP中的类继承

I'm not sure if the title of this question is accurate, so please bear with me.

I'm creating a lightweight MVC framework. It works a bit like this:
Main.php (the main class) invokes

  • Controller extends Main ($this->controller) and
  • Model extends Main ($this->model)

Now the model and controller class will invoke controllers and models depending on the URI (For example, Blog.php [extends Controller]).

(So for recap: Main.php invokes $this->model and $this->controller. Controller class invokes Blog extends Controller).

From the Blog.php, how can I access the functions in Controller, and the functions in Main?

I ask this because in Controller I will need to access functions in my URI class but I cannot do that, as PHP tells me that object doesn't exist when I do $this->uri.

It seems to be bad practice to do $this->controller, $this->model, and $this->uri in every class.

A friend of mine suggested I use a Magic Method called __get(). Does this seem right?

class Main
{  
    function __construct() {  
        loadClasses();  
        loadUri();  
    }  

    function loadClasses() {  
        $this->controller = new Controller();  
        $this->uri = new Uri();  
        $this->load = new Load();  
    }  

    function loadUri() {  
        //Using the uri, determine what Controller to load.  
    }  
}  

class Controller  
{  
    ...  
}  

class Blog extends Controller  
{  
    $this->load->view(); //Should access the view function in the load class
}  

A couple of things here. First Main.php should "invoke" a instance of the controller and not the model. It is the controller's job to connect the model and the view.

Now normally all your controllers will derive off of some BaseController. As Criticus pointed out in the comments, at any time you can access any protected or public methods from the parent by using parent::. BaseController should contain everything that is common amoung all your controllers.

As far as how to work with your URI class. Normally before all of this, main.php is using some sort of routing class to determine which controller to use. Any URI work should be done here and URI. This would also reduce your reliance on magic methods.

if you are interested, get in contact with me and I would be happy to share with you a simple mvc framework I developed for php projects.