简体   繁体   中英

PHP Declare class with a variable

Im trying to execute the following code

$our_controller = new App\Controllers\$routing->_new['controller']();

But Im getting 'Unexpected $routing'

And this is the class Im trying to load looks like

namespace App\Controllers;

class HomeController extends BaseController
{
      public function __construct()
      {
          parent::__construct();
      }
}

If I type the following

$our_controller = new App\Controllers\HomeController();

Works perfectly but I must use the variable instead of HomeController

Try this, I think it should work:

$namespace = 'App\Controllers\' . $routing->_new['controller']();

$our_controller = new $namespace;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM