问题描述:

I have a simple php code and I want to split it into model,view,helper. Model should access some methods from helper class and helper class should access some methods from model class.

I am not sure if the below pattern is correct. I guess it is not because in this example model,view,helper will be initialized multiple times. Which is the most simple way to accomplish something like I am trying to do with the below code?

lib/main.php

require_once('lib/model.php');

require_once('lib/helper.php');

require_once('lib/view.php');

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'show';

switch($action){

case "show":

$class->showAction();

break;

case "another":

$class->anotherAction();

break;

}

class main extends abstract{

public function showAction(){

if($this->helper->getParam('browse')){

//something

}else{

$profiles= $this->model->getProfiles();

}

echo $this->view->toHtml($profiles);

}

}

lib/abstract.php

class abstract{

public function __construct(){

$this->model = new model();

$this->view = new view();

$this->helper = new helper();

}

}

lib/model.php

class model extends abstract{

public function getProfiles(){

if($this->helper->someMethod(){

//some code

}

//some code

return $profiles;

}

}

lib/helper.php

class helper extends abstract{

public function someHelperMethod(){

if($this->model->someAnotherMethod(){

//some code

}

//some code

return $profiles;

}

}

网友答案:

First problem is that you are nesting your classes like russian dolls. You shouldn't have your Abstract class both contain model/view/helper, and be the parent of model/view/helper.

I'd caution against using extension just to ensure a class is in-scope.

Generally you can think of it this way: use extension when your class has shared behaviors or properties as it's parent, but it either needs additional functionality, or modifications to existing functionality.

The "abstract" class you defined shares no attributes or methods between Model/View/Helper, so Model/View/Helper should not probably extend from it.

If however you want a "container" class that contains instances of each of these class types, just make it a standalone class, don't extend it, for example:

class Container{
    public $model;
    public $view;
    public $helper;

    public function __construct(){
        $this->model = new model();
        $this->view = new view();
        $this->helper = new helper();
    }

    public function showAction(){
        if($this->helper->getParam('browse')){
            //something
        }else{
            $profiles= $this->model->getProfiles();
        }
        echo $this->view->toHtml($profiles);
}

Then instantiate it only once at the start someplace:

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'show';
$class = new Container();

Then, if you want to call something from Model inside Helper, this can be done a number of ways.

One option, pass a reference to this class and keep it inside Helper:

// Inside Container
public function __construct(){
    $this->model = new model();
    $this->view = new view();
    $this->helper = new helper($model);
}

The Helper class would look like:

class Helper{
    protected $model; 

    public function __construct($model){
        $this->model = $model;  
    }

    public function someHelperMethod(){
        if($this->model->someAnotherMethod()){
           //some code
           }
            //some code
        return $profiles;
    }
}
相关阅读:
Top