I'm developing application using MVC concept and want to restrict the View class from calling methods of model but give it access to model's properties so it can just get data when it needs it. Instead i want to give the Controller class ability to call model's methods(and if possible restrict it from accessing properties cause it really doesn't need to do so).
My question is how do i design classes relationship to achieve that goal or can i just somehow tell php to disallow that class calling methods of another?
Currently relations of my classes look like this:
Controller
class Controller
{
protected $_model; //has access to model so it can call model's methods to modify it
protected $_view;
public function __construct($model, $view)
{
$this->_model = $model;
$this->_view = $view;
}
}
View
class View
{
protected $_model; //has access to model so it can get model's properties when needed
public function __construct($model)
{
$this->_model = $model;
}
}
And there are other classes which are inheriting from Controller and View class, like:
class UserController extends Controller
{
public function modifyData()
{
$this->_model->modifyX(1); //this works and it should be working because i need the controller to be able to call model's methods
$someVar = $this->_model->x; //this works and it should not be working because i don't need the controller to be able to get model's properties
}
}
and
class UserView extends View
{
public function getData()
{
$this->_model->modifyX(1); //this works and it should not be working because i don't need the view to be able to call model's methods
$someVar = $this->_model->x; //this works and it should be working because i do need the view to be able to get model's properties
}
}
So what do you suggest as the best method of achieving this?