3

what do you call the feature of Python where:

  1. You assign a function to a variable

    myPowerVariable = pow

    mySqrtVariable = math.sqrt

  2. Then you call that variable with arguments?

    myPowerVariable(2,3)

    prints 8

    mySqrtVariable(9)

    prints 3

If you try that in PHP, it won't work, hoping somebody can tell me what do you call this feature.

Thank you

arvil
  • 840
  • 11
  • 27
  • look at [python functions as objects](http://stackoverflow.com/questions/25292239/python-functions-are-objects) – kmad1729 Mar 15 '17 at 18:29
  • 2
    Maybe you mean "first class objects" http://stackoverflow.com/questions/27392402/what-is-first-class-function-in-python – VPfB Mar 15 '17 at 18:31
  • @VPfB these knowledge you won't obtain from tutorials but only if you study computer science? – arvil Mar 15 '17 at 18:45
  • 1
    @TheWolf I don't know the answer. But I'd like to take use of this tiny comment to recommend good books. They give better "foundation" as tutorials. – VPfB Mar 15 '17 at 19:36
  • @VPfB please go ahead, thank you – arvil Mar 15 '17 at 20:35

3 Answers3

3

First off, you have your wording mixed up: you are actually assigning a function to a variable.

Anyways, this is an example of Python having first class functions.

degraafc
  • 56
  • 4
2

In PHP they are called Variable functions and work fine, assuming you use proper PHP syntax:

$myPowerVariable = 'pow';
echo $myPowerVariable(2, 3);  // prints 8

If you did:

$myPowerVariable = pow();

You get:

Warning: pow() expects exactly 2 parameters, 0 given

Because you are executing pow() and assigning the result to $myPowerVariable but not providing the required arguments.

This is a simple example of an Anonymous function that you mention in the comments:

$myPowerVariable = function($a, $b) {
    echo pow($a, $b);
};

$myPowerVariable(2, 3);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • I never understood anonymous functions in PHP, is that all I need to know about anonymous function? – arvil Mar 15 '17 at 18:33
1

This is in general referred to as functional programming, and the feature does exist in PHP though there are some differences in syntax. The following works in PHP:

function myFunction() {
    echo 'here!';
}

$myVar = 'myFunction';

$myVar();

This also works in PHP:

class myClass
{
    function myMethod() {
        echo 'here!';
    }
}

$myVariable = ['myClass', 'myMethod'];

call_user_func($myVariable);
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • isn't that scary that a `string` you assigned to a variable might actually be a `function`, then suddenly you have a variable that can call a `function`? Or I am just overthinking? – arvil Mar 15 '17 at 18:34
  • 1
    @TheWolf It could be a bit more dangerous as, in the Python example the assignment line would throw NameError if the function isn't defined, but in PHP the error wouldn't be raised until you try to call the variable function, since assigning a string to a variable is always allowed. The PHP version could lead to runtime errors not detectable during compilation, depending on how it is used. – Jeff Lambert Mar 15 '17 at 18:38