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?

2
  • possible duplicate of Class not found PHP OOP Commented Jul 19, 2012 at 14:14
  • Have you turned on full error reporting on your local server ? Commented Jul 19, 2012 at 14:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!!!!
1

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

Comments

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 ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.