note 129898 added to functions.arrow

From: Date: Thu, 05 Dec 2024 00:17:26 +0000
Subject: note 129898 added to functions.arrow
Groups: php.notes 
Request: Send a blank email to php-notes+get-221701@lists.php.net to get a copy of this message
If you're a JavaScript developer, here are the similarities and differences to JS arrow functions: Same: - Makes an anonymous function - Binds the value of "$this" to its value in the parent scope. - (along with all other variables of the parent scope. See note below) Different: - You must write "fn()" instead of just "()" - The function body is limited to just ONE expression - So no multi-line function bodies with "{" and "}" Same and Different at the same time: - Binds ALL the variables of the parent scope - In JavaScript all functions are closures, binding to the variables in their parent scope (except for "this"). - But in PHP, normal anonymous functions (defined with "function() {}") do NOT get access to the parent scope, unless they explicitly declare a closure with keyword "use" - PHP arrow functions, on the other hand, automatically bind to ALL variables in the parent scope. So, this makes them behave the same as JS functions, but be aware that in PHP this is special behavior unique to arrow functions. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 2407:7000:98d8:6600:e0ce:ba9b:aacf:3584) ---- Manual Page -- https://php.net/manual/en/functions.arrow.php Edit -- https://main.php.net/note/edit/129898 Del: integrated -- https://main.php.net/note/delete/129898/integrated Del: useless -- https://main.php.net/note/delete/129898/useless Del: bad code -- https://main.php.net/note/delete/129898/bad+code Del: spam -- https://main.php.net/note/delete/129898/spam Del: non-english -- https://main.php.net/note/delete/129898/non-english Del: in docs -- https://main.php.net/note/delete/129898/in+docs Del: other reasons-- https://main.php.net/note/delete/129898 Reject -- https://main.php.net/note/reject/129898 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221701) next »