1

this supposed to be an MVC framework (i am learning by doing)

class load{
    public function model(){
        // some code...
        [...] = model[$modelName] = new $modelName();
    }
}

this class handles all load option in to the controller..

class _framework{
    public $load; // object
    public $model; // array

    function __construct(){
        //some code...
        $this->load = new load();
    }
}

this is the framework of the controllers the controller extends this class.

edit:

it should use like this:

class newController extends _framework{
    public function index(){
        $this->load->model('modelName'); // for loading the model.
        $this->model['modelName']->modelMethod(); // for use the model method.
    }
}

my problem is where the [...]. how can I get the new model to the array in the framework class??

tereško
  • 58,060
  • 25
  • 98
  • 150
GoDLighT
  • 148
  • 10

1 Answers1

0

If you want to get an array out of your model object, you can define its public method toArray:

class modelName {
    public function toArray () {
        $array = ...; // get your array here
        return $array;
    }
}

Then you can call it from outside and get your array:

$myArray = $myModel->toArray();

Your model should encapsulate its data and make them accessible via API like that.

I would not call an array a model though. A model is a layer with many classes serving the purpose of the model - storing your data, peforming their validation, whatever other data-related business logic and providing API to access the data.

Also it is common to capitalize your classes.

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
  • the model is array of all the models i load in the controller, eventually $model is an array thet contain models objects. this is still worng? – GoDLighT Sep 29 '13 at 22:29
  • It is always good to minimize your coupling, so your controller should not know the precise structure of your models, whether it is an array or anything else. If you need an array of models, I would encapsulate them into a new model and provide the API needed for the controller. – Dmitri Zaitsev Sep 29 '13 at 22:45