0

I am new to PHP. I am trying to read the code in Symfony 2 app, and I am not able to understand what is this function.

Can anyone please explain to me what this function is doing. I am not able to get how is this variable defined

$def = function($name, $class, $args=[]) use ($container){
   return $container->doStuff($name, $class);
};

What is use doing there?

nbro
  • 15,395
  • 32
  • 113
  • 196
user191542
  • 275
  • 1
  • 7
  • 17

2 Answers2

0

Use keyword used in this way

<?php
namespace foo;
use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
?>
0

This:

$def=function($name, $class, $args=[]) use ($container){
   return $container->doStuff($name, $class);
};

is roughly the same than:

$def = my_function($container, $name, $class);

function my_function($container, $name, $class, $args=[]) {
  return $container->doStuff($name, $class);
}
François Constant
  • 5,531
  • 1
  • 33
  • 39