Jump to content

Recommended Posts

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 $controllerModel $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 ??

Link to comment
https://forums.phpfreaks.com/topic/281561-oops-confusion/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/281561-oops-confusion/#findComment-1446791
Share on other sites

@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.. ?

Link to comment
https://forums.phpfreaks.com/topic/281561-oops-confusion/#findComment-1446873
Share on other sites

@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

Link to comment
https://forums.phpfreaks.com/topic/281561-oops-confusion/#findComment-1446878
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.