The difference between "using" and passing a parameter to a controller function

I donโ€™t have a specific problem, I just want to deepen my understanding of what is happening with Silex and some new PHP features in general. This is based on code examples on the "use" page of the Silex documentation:

$blogPosts = array(
    1 => array(
        'date'      => '2011-03-29',
        'author'    => 'igorw',
        'title'     => 'Using Silex',
        'body'      => '...',    );

$app->get('/blog/{id}', function (Silex\Application $app, $id) use ($blogPosts) {
    //do stuff
}

Questions

  • What is the difference between passing $appand $idas parameters for a function, and use - for a variable$blogPosts

  • Can it $blogPostsalso be passed as a parameter to a function?

  • In addition, I often see use ($app). What is the difference between using -ing $appand passing it as a parameter?
+5
2

, " PHP". ( ), $app $id, $blogPosts.

<?php
$a = "a";
$b = "b";
$c = function ($d) use ($b) {
    echo $d . "." . $b . PHP_EOL;
};
$b = "c";
$e = function ($d) use ($b) {
    echo $d . "." . $b . PHP_EOL;
};

$c($a); // prints a.b, and not a.c
$e($a); // prints a.c

$b, , , , .

+7

.

get() $blogPosts, .

0

All Articles