0

We have a small mvc we have built, we want to access models from different controllers.

i.e

class Controller {
    function A() {
    }
}

class Search extends Controller {
    function B() {
        $this->model->doSomething();    
    }
}

class Profile extends Controller {
    function B() {
        ***** HERE ***** ?????????????????????????????????????????????
        Search::doSomething();
    }
}

class Model {

}

class search_model extends Model {     
    public function doSomething() {
        // Do Something
        echo "doing something";
    }
}

class profile_model extends Model { 
    public function getProfile() {
        // Get Profile
        echo "getting profile";
    }
}

I want to access when in the Profile Controller the Search Controller function doSomething() if you can see like the HERE section or something along them lines?

2 Answers2

0

Just include the model in the other controller. THere is nothing wrong with acessing the same model from several controllers

FabioCosta
  • 3,069
  • 6
  • 28
  • 50
0

Controller is the parent class of both Search and Profile, so you can add a function you can re-use for both.

class Controller {

    public $model;

    public function doSomething() {
        if($this->model === null)
        {
            $this->model = new search_model();
        }
        return $this->model->doSomething();
    }
}
Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50