0

I am having a problem on my MVC framework. Here is my loadModel() method inside the main controller:

public function loadModel($name) {
$path = 'models/' . $name . '_Model.php';

    if (file_exists($path)) {
        require $path;  
        $modelName = $name . '_Model';
        $this->model = new $modelName;
    }
}

As you can see, when i try to load a controller, it automatically loads a model with the same name + _model.php.

I do not have any problem, when I use wamp or xampp, but, when I uploaded it in my website, it says "Undefined property" on every model. That means the model is not loaded. I know the problem is in there...

Is that some kind of an error in php.ini file of the server? Or maybe it because of different PHP versions?

tereško
  • 58,060
  • 25
  • 98
  • 150
Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83

3 Answers3

2

Windows is not case sensitive, i.e. Model.php and model.php are the same as far as windows is concerned. ON unix/linux however (which I'm assuming your website server is) the file system IS case sensitive.

Basically, check your files names - if you're trying to load $name_Model.php but the file is $name_model.php it's going to fail when you upload it.

Dan Smith
  • 5,685
  • 32
  • 33
  • I think you are right!! i am loading it like for example login_Model, whose class is Login_Model now I Know!!! thank you, gotta test it now!!!! – Joey Hipolito Jul 19 '12 at 13:16
1

You might wanna consider using autoloading, that fixes all your problems:

Sherlock
  • 7,525
  • 6
  • 38
  • 79
1

Don't rely on relative paths, they depend on the current directory.

Make an absolute path like (adapt to your needs):

$path = __DIR__ . '/models/' . $name . '_Model.php';

If you get something like that to work locally, it should work on the server too.

BTW, i don't understand why _Model suffix. Wouldn't models/Car.php be sufficient ?

jpic
  • 32,891
  • 5
  • 112
  • 113