Application Names and Scopes
When you build a Yee application you will enter various scopes in your code (e.g. global scope and function scope). You will likely need a reference to your Yee application in each scope. There are several ways to do this:
- Use application names with the Yee application's
getInstance()
static method - Curry an application instance into function scope with the
use
keyword
Application Names
Every Yee application may be given a name, however this is optional and not a requirement. Names help you get a reference to a Yee application instance in any scope throughout your code. Here is how you set and get an application’s name:
<?php
$app = new \Yee\Yee();
$app->setName('foo');
$name = $app->getName(); // "foo"
Scope Resolution
So how do you get a reference to your Yee application? The example below demonstrates how to obtain a reference
to a Yee application within a route callback function. The $app
variable is used in the global scope to define
the HTTP GET route. But the $app
variable is also needed within the route’s callback scope to render a template.
<?php
$app = new \Yee\Yee();
$app->get('/foo', function () {
$app->render('foo.php'); // <-- ERROR
});
This example fails because the $app
variable is unavailable inside the route callback function.
Currying
We can inject the $app
variable into the callback function with the use
keyword:
<?php
$app = new \Yee\Yee();
$app->get('/foo', function () use ($app) {
$app->render('foo.php'); // <-- SUCCESS
});
Fetch by Name
You can use the Yee application's getInstance()
static method, too:
<?php
$app = new \Yee\Yee();
$app->get('/foo', 'foo');
function foo() {
$app = Yee::getInstance();
$app->render('foo.php');
}