2
$test = function(){};

It's a new feature of php ver 5.3. I'm interested to know what's the reason.

Mostafa -T
  • 730
  • 8
  • 19
  • 2
    Hmm, that's not correct syntax and as off 5.4 should fail because of unexpected `your_function`. In fact, it expects `(args)` to be followed after `function` keyword. – Leri Feb 19 '14 at 10:50
  • 2
    Oh, that's an error. you can do `$test = function (){};`, not `$test = function your_function(){};`. Second syntax that's not allowed – Alma Do Feb 19 '14 at 10:53
  • 2
    With your edit - answer is obvious and comes from what are closures and what are their intentions – Alma Do Feb 19 '14 at 10:57

1 Answers1

-1

This is called variable functions in php. We can define some functions and can assign it into variables.This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

<?php
function foo() {
    echo "In foo()<br />\n";
}

function bar($arg = '')
{
    echo "In bar(); argument was '$arg'.<br />\n";
}

// This is a wrapper function around echo
function echoit($string)
{
    echo $string;
}

$func = 'foo';
$func();        // This calls foo()

$func = 'bar';
$func('test');  // This calls bar()

$func = 'echoit';
$func('test');  // This calls echoit()
?>

Source

Check these too

Community
  • 1
  • 1
Lal krishnan S L
  • 1,684
  • 1
  • 16
  • 32
  • 2
    Sorry, this answer is wrong. This was always possible (at least since PHP 4). You are just using the variable name to call a function. We are talking about a function residing in variable. – akirk Feb 19 '14 at 10:54
  • May I know the reason for down vote? – Lal krishnan S L Feb 19 '14 at 10:54