{"id":354,"date":"2019-11-29T05:32:12","date_gmt":"2019-11-29T05:32:12","guid":{"rendered":"http:\/\/appfinz.com\/blogs\/?p=354"},"modified":"2025-05-15T05:14:46","modified_gmt":"2025-05-15T05:14:46","slug":"php-approves-short-arrow-function","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/php-approves-short-arrow-function\/","title":{"rendered":"PHP Approves Short Arrow Functions"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/www.php.net\/credits.php\">PHP team<\/a> recently approved the Short Arrow Functions RFC proposed by Nikita Popov, Levi Morrison, and Bob Weinand.<\/p>\n\n\n\n<p>In the RFC it shows this as an example to give you an idea on how it can be used:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$extended = function ($c) use ($callable, $factory) {\n    return $callable($factory($c), $c);\n};\n \n\/\/ with arrow function:\n\/\/ Appfinz Technologies \n$extended = fn($c) =&gt; $callable($factory($c), $c);\n\n A Laravel example could look like this: \n\n\/\/ Current\n$users-&gt;map(function($user) {\n    return $user-&gt;first_name.' '.$user-&gt;last_name;\n});\n\n\/\/ with arrow function:\n\/\/ appfinz Technologies \n$users-&gt;map(\n    fn($user) =&gt; $user-&gt;first_name.' '.$user-&gt;last_name\n);<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Latest on PHP Short Arrow Functions<\/h3>\n\n\n\n<p><strong>1. Syntax Simplification (PHP 7.4+)<\/strong><br>PHP introduced short arrow functions in version <strong>7.4<\/strong> to reduce verbosity when writing closures.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ Traditional anonymous function\n$double = function ($x) {\n    return $x * 2;\n};\n\n\/\/ Short arrow function\n$double = fn($x) =&gt; $x * 2;\n<\/pre><\/div>\n\n\n\n<p><strong>2. Implicit <code>use<\/code> Handling<\/strong><br>Short arrow functions automatically <em>capture variables<\/em> from the parent scope \u2014 unlike traditional closures where you need <code>use()<\/code> explicitly.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$multiplier = 3;\n\n\/\/ Short arrow captures $multiplier automatically\n$triple = fn($x) =&gt; $x * $multiplier;\n<\/pre><\/div>\n\n\n\n<p><strong>3. Single Expression Limitation<\/strong><br>Short arrow functions <strong>only support a single expression<\/strong> \u2014 no blocks, conditionals, or multiple statements.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ \u2705 Valid\n$sum = fn($a, $b) =&gt; $a + $b;\n\n\/\/ \u274c Invalid - cannot use multiple statements or blocks\n\/\/ $func = fn($x) =&gt; { $y = $x + 1; return $y; };\n<\/pre><\/div>\n\n\n\n<p><strong>4. Use Cases Growing in Modern PHP<\/strong><br>They&#8217;re now widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Laravel collections<\/li>\n\n\n\n<li>Functional programming patterns<\/li>\n\n\n\n<li>Array mapping\/filtering<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$data = [1, 2, 3, 4];\n$filtered = array_filter($data, fn($n) =&gt; $n % 2 === 0);\n<\/pre><\/div>\n\n\n\n<p><strong>5. Performance &amp; Readability<\/strong><br>They are <strong>not faster<\/strong>, but significantly improve <strong>code clarity and brevity<\/strong>, especially in inline functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc Summary<\/h3>\n\n\n\n<p>PHP short arrow functions (<code>fn() =&gt;<\/code>) are great for cleaner, simpler, and modern-looking code. They automatically inherit scope variables and are ideal for one-liners, making PHP more competitive with modern functional paradigms seen in JavaScript, Python, etc.<\/p>\n\n\n\n<p>If you&#8217;re using <strong>PHP 7.4 or newer<\/strong>, it&#8217;s highly recommended to adopt them wherever applicable \u2014 especially in array operations, callbacks, and simple logic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PHP team recently approved the Short Arrow Functions RFC proposed by Nikita Popov, Levi<\/p>\n","protected":false},"author":1,"featured_media":357,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[53,97],"class_list":["post-354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php","tag-php-7-4"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":7,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/354\/revisions\/1386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/357"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}