Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

anonymous functions or function

Look at the following approaches:

$app->get('/', function () use ($app) {

    // ... code here!...
    echo $app->view->render('index', $params);

})->setName('home');

or

$app->get('/', 'indexAction')->setName('home');

function indexAction(){
    global $app;
    // ...code here!!!..
    echo $app->view->render('index', $params);
}

Which is the best option? , Okay use 'global' ?, please I need some advice.

edited Aug '16

From 3.0.0 You can just use:

$app->get('/', function () {

    // ... code here!...
    echo $this->view->render('index', $params);

})->setName('home');

Definitely DONT USE GLOBAL.

If you want to seperate somehow those things then you can use handlers:

https://docs.phalconphp.com/pl/latest/reference/micro.html#using-controllers-as-handlers

From 3.0.0 You can just use:

```php $app->get('/', function () {

// ... code here!... echo $this->view->render('index', $params);

})->setName('home'); ```

Definitely DONT USE GLOBAL.

Why not use Global? what are the reasons?



99.5k
Accepted
answer
edited Aug '16

Beacause it's ancient and it was mostly used in php 4.....

There is just no reason for using it in php 7. Global keyword stayed here for years for compability reasons.

I have reviewed the issue, and you are right. thanks man!