1
votes

I have this main controller:

namespace App\Core;
/**
* Controller class
*/

class Controller
{
    /** @var View View The view object */
    public $View;
    public $templates;
    /**
     * Construct the (base) controller. This happens when a real controller is constructed, like in
     */
    public function __construct()
    {

    }

    public function loadModel($name) {

        $path = '\\App\\Front\\Model\\'.$name; // Not Work

        //$this->model = new \App\Front\Model\IndexModel(); Work

        $this->model = new $path;

        return $this->model;

    }
}

In IndexController I have:

namespace App\Front\Controller;

use App\Front\Model\IndexModel;

class IndexController extends \App\Core\Controller {

    public function index(){
        $this->loadModel('IndexModel()')->test();
    }
}

Now in action I see error:

Fatal error: Uncaught Error: Class '\App\Front\Model\IndexModel()' not found in /Applications/MAMP/htdocs/mvc/application/Core/Controller.php:64

how do fix error?

1
@MagnusEriksson: fix!! thanksharmony talk
I made an answer. Since it worked for you, feel free to accept it so others know the problem have been resolved.Magnus Eriksson

1 Answers

0
votes

You need to remove the () from the string:

$this->loadModel('IndexModel')->test();

It's not necessary to have () when creating a new instance without arguments. If you do want it anyway, or you want to pass some arguments, you need to add them after the variable on instantiation instead:

new $path('someArgument');