getherbert / framework

Core Framework
http://getherbert.com/
41 stars 54 forks source link

What is the Herbert\Framework\Application::loadPluginX() function for? #1

Closed unstoppablecarl closed 9 years ago

unstoppablecarl commented 9 years ago

Could you explain what this function does, especially the variable variable $$x ?

https://github.com/getherbert/framework/blob/dev/Herbert/Framework/Application.php#L170

jasonagnew commented 9 years ago

@unstoppablecarl hopefully this explains it a little.

$this->loadPluginX(
    'router',
    array_get($config, 'routes', [])
);

//Sets $x equals "router" there $$x creates a variable $router 

$this->loadPluginX(
     'enqueue',
     array_get($config, 'enqueue', [])
 );

//Sets $x equals "enqueue" there $$x creates a variable $enqueue 

The reason for this to allow files to be loaded like routes.php have access to $router:

// $router = Herbert\Framework\Router
$router->get();
unstoppablecarl commented 9 years ago

I've never used variable variables. This is the first time I've seen a practical use. This is just for convenience, you could get the router by doing $container['router'] right?

ConnorVG commented 9 years ago

Indeed, it is for convenience. Helps remove a lot of repeated code (by having a function for each one that calls loadPluginX) :smile:

$container['router'] would return the router instance, yeah