1

Sorry if this has already been answered, I am new to OOP in PHP. I'm trying to build an mvc for educational and testing purposes and to increase my knowledge of it. I'm stuck at one point, and can't figure it out. I'm sure it's a simple solution, but I don't know what the correct term/word would be to look it up.

If an answer already exists, please let me know.

Here is what I am trying to do:

class Load {
  public function __construct(){
  }

  public function model($file_name){
    include($file_name.'.php');
  }
}

class Home_model extends M_F_Model{
  function __construct() {
    parent::__construct();
  }

  public static function test_this_model($var){
    $result = $var.'+2';
    return $result;
  } 
}

class M_F_Controller {
  public $load;

  public function __construct(){
    $this->load = new Load();
  }
}

class Home extends M_F_Controller{
  function __construct(){
    parent::__construct();
    $this->load->model('home_model');       
  }

  function index($var){
    $result = Home_model::test_this_model($var);
    return $result;
  }
}

I would like to turn Home_model::test_this_model(); into $this->home_model->test_this_model(); without having to declare the instance of home_model in the Home class for each class I may want to include in the future, and to avoid using static functions.

Thanks in advance, let me know if you need any further information.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
klye_g
  • 1,242
  • 6
  • 31
  • 54

2 Answers2

1

Is there an issue with doing:

$this->home_model = $this->load->model('home_model');

Inside of Home's constructor, and then instead modifying the model function of your loader class to return a new instance instead of just including the file? Something like:

public function model($file_name) {
    include $file_name . '.php';
    $model = new $file_name();
    return $model;
}

(this assumes that $file_name corresponds to the class name inside of the file)

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • That is a possibility. Thank you for the answer. I was trying to keep it as $this->load->model('filename here'); and still be able to do $this->home_model->func(); But if that isn't possible then this way works. I just started learning a few days ago..I was trying to mimic CodeIgniter without directly copying.. – klye_g Aug 14 '12 at 01:03
  • Last time I checked CI was open-source. It doesn't hurt to take a peak :) – Logan Serman Aug 14 '12 at 02:01
  • @LoganSerman , the *openness* of CodeIgiter is actually quite questionable, since it is largely controlled by EllisLab. – tereško Aug 14 '12 at 02:06
1

If you are learning MVC and OOP from CodeIgniter, then you are doing it wrong™. You should not even be touching MVC pattern before you have quite advanced understanding or OOP, because it is quite complicated and requires you to understand such things as LoD, SoC, IoC and all of SOLID principles.

Model is not a class or an object. It is a layer. The MVC pattern is made from two layers: model layer and presentation layer. Presentation layers is mostly made from views, controllers and templates, it provides the user interface and handles how users interact with it.

The controller is supposed to be responsible for changing, based on user's input, the state of model layer and current view. Not for creating objects and rendering templates. And they are not supposed to return anything.

The basic workflow in controller should be :

  • change state of current view
  • acquire structure from model layer
  • execute method on that structure

Answer: unless you want to bind each controller to specific structures, you will need two lines for dealing with model layer.

What you think of as "models" are actually domain objects, which are part of model layer. You really should not access them directly, but people, who are just starting to learn MVC pattern, usually do. I suspect that for your current level, this answer would provide the most benefit, but it is not how I understand model in MVC. For the advanced version, read this.

Also, loading each class manually is quite outdated practice. Since PHP 5.1 we have spl_autoload_register(), which lets you create a shared autoloader, that is triggered, when you try to use unknown class.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150