We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.phalcon.io/pl/latest/reference/micro.html#using-controllers-as-handlers

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.

Why not use Global? what are the reasons?



145.0k
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!