Joey Hipolito
Joey Hipolito

Reputation: 3166

Why do I need to explicitly instantiate an object?

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?

Upvotes: 0

Views: 80

Answers (3)

jpic
jpic

Reputation: 33410

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 ?

Upvotes: 1

Sherlock
Sherlock

Reputation: 7597

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

Upvotes: 1

Dan Smith
Dan Smith

Reputation: 5685

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.

Upvotes: 2

Related Questions