Jaswinder Posted August 26, 2013 Share Posted August 26, 2013 Hello friends ,, i have less experience in OOPS till now.. i get confused in the following code.. class Model { public $text; public function __construct() { $this->text = 'Hello world!'; } }class View { private $model; private $controller; public function __construct(Controller $controller, Model $model) { $this->controller = $controller; $this->model = $model; } public function output() { return '<h1>' . $this->model->text .'</h1>'; } }class Controller { private $model; public function __construct(Model $model) { $this->model = $model; }} The problem is In View class constructor arguments ,.. what this means (Controller $controller, Model $model) the code is using the Controller class and Model class ??? is it an alternate to inheritence ?? and also View class is using $text in output() function,, how ?? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted August 26, 2013 Share Posted August 26, 2013 what's happeing is that the constructor class - called as soon as a new class object is loaded - is loading the lother two classes (Controller and Model) into local parameters $controler and $model. These parameters become instances of the other classes respectivly. so if $view = new Veiw; then $view->$model is an instance of the model class. That's why the View class has access to $text parameter from inside it's self. This is an implementation of a form of dependancy injection. You could imagine it as a form of reverse inheritance (that's not an actual deffinition - just an easy way to visualise the process). Basicly what you do is you have each class object create localised instances of the other classes it needs to have access to in order to operate, rather than the other way around. Analogy (I suck at these, but I'll give it a shot) : If you imagin inheritance as : A supermarket has customers. so the supermarket employes somone to make sure that as every customer walks through the door they are given a trolly/cart, a voucher book, and a bunch of bags to pack there shopping in. Now Dependancy Injection (DI) : Each person that goes to the supermarket to by something has access to, and chooses only what they need from, all of the above and can pick and choose what they themselves need to use from it. So now people who only want a pint of milk don't need to pick up a trolly/cart, and don't need to get any vouchers - since none of the vouchers are for milk, but can still get a bag to pack it in. Instantly you reduce the amount of resources used by customers as only the ones that need to use the trolly/cart, or benefit from using the vouchers are going to take them. Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted August 26, 2013 Author Share Posted August 26, 2013 nice explanation.....i got it . thanx Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted August 26, 2013 Share Posted August 26, 2013 no worries, glad I could help. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 26, 2013 Share Posted August 26, 2013 (edited) Your MVC implementation is wrong. class Controller { private $view; private $model; } class Model { } class View { private $model; }Your controller connects your Model and View. Not your View. Edited August 26, 2013 by ignace Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted August 26, 2013 Author Share Posted August 26, 2013 @ignace... so it means, the constructor which is called in View class.. is replaced with Controller class construtor..?? if u dont mind... do u have any good link on MVC.. which clearly explain this MVC concept... i read it from the following link. http://r.je/mvc-in-php.html is it good explaination or do u have better one.. ? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 26, 2013 Share Posted August 26, 2013 @ignace... so it means, the constructor which is called in View class.. is replaced with Controller class construtor..?? if u dont mind... do u have any good link on MVC.. which clearly explain this MVC concept... i read it from the following link. http://r.je/mvc-in-php.html is it good explaination or do u have better one.. ? And it seems as if the guy has some opinions on it as well: http://r.je/views-are-not-templates.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.