Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function object
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== In PHP == [[PHP]] 5.3+ has [[first-class function]]s that can be used e.g. as parameter to the {{Code|usort()}} function: <syntaxhighlight lang="php"> $a = array(3, 1, 4); usort($a, function ($x, $y) { return $x - $y; }); </syntaxhighlight> [[PHP]] 5.3+, supports also lambda functions and closures. <syntaxhighlight lang="php"> function Accumulator($start) { $current = $start; return function($x) use(&$current) { return $current += $x; }; } </syntaxhighlight> An example of this in use: <syntaxhighlight lang="php"> $a = Accumulator(4); $x = $a(5); echo "x = $x<br/>"; // x = 9 $x = $a(2); echo "x = $x<br/>"; // x = 11 </syntaxhighlight> It is also possible in PHP 5.3+ to make objects invokable by adding a magic {{Code|__invoke()}} method to their class:<ref name="phpinvoke">[http://php.net/manual/en/language.oop5.magic.php#object.invoke PHP Documentation on Magic Methods]</ref> <syntaxhighlight lang="php"> class Minus { public function __invoke($x, $y) { return $x - $y; } } $a = array(3, 1, 4); usort($a, new Minus()); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)