Bifalgorithm post · Computer science one · Generalized Rounding

How can functions be assigned to variables and used as callbacks in PHP?

How can functions be assigned to variables and used as callbacks in PHP? (Computer science one · Generalized Rounding) — "40.1.6. Function Pointers Functions a…

Ideas in this flowchart

  • Pointeurs de fonction
  • Les fonctions sont des morceaux de code situés en mémoire, comme les variables.
  • Le passage des fonctions par référence est analogue au passage des variables par référence.
  • En PHP, les fonctions sont considérées comme des citoyens de première classe.
  • L’identifiant de swapByRef() peut être affecté à la variable $func.
  • La variable $func contient une référence vers la fonction swapByRef().
  • La fonction peut être invoquée avec $func($x, $y).
  • Cette utilisation permet de traiter les fonctions comme des callbacks.
  • Certaines définitions plus restrictives ne considèrent pas les fonctions comme des citoyens de première classe dans ce sens.
Feed/

How can functions be assigned to variables and used as callbacks in PHP?

No cover

Channel

Computer science one

1 page

"40.1.6. Function Pointers Functions are just pieces of code that reside somewhere in memory just as variables do. Since we can pass variables by reference, it also makes sense that we would do the same with functions. In PHP, functions are first-class citizens 2 meaning that you can assign a function to a variable just as you would a numeric value. For example, you can do the following. 1 $func = swapByRef; 2 3 $func($x, $y); In the example above, we assigned the function swapByRef() to the variable $func by using its identifier. The variable essentially holds a reference to the swapByRef() function. Since it refers to a function, we can also invoke the function using the variable as in the last line. This allows you to treat functions as callbacks to other functions. We will revisit this concept in Chapter 47. 2 Some would use a much more restrictive definition of first-class and would not consider them first-class citizens in this sense"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.