[php-src] Issue #15622: Anonymous Functions/Methods Inside Of Classes

From: Date: Wed, 28 Aug 2024 18:50:56 +0000
Subject: [php-src] Issue #15622: Anonymous Functions/Methods Inside Of Classes
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-247621@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/15622 Author: trymeouteh ### Description The ability to create anonymous function/method inside of a class. This can allow for the creation of classes that can simply be executed without pointing to a named property Here is how the syntax could look like. You have a class with two protected methods which are used in the public method. The public method is an anonymous method and is called by executing the class itself, and not executed by executing a property in the class. ``` <?php class myClass { private static function add($a, $b) { return $a + $b; } private static function subtract($a, $b) { return $a - $b; } public static function() { $add = self::add(2, 2); $subtract = self::subtract(9, 3); return $add . PHP_EOL . $subtract . PHP_EOL; } } echo myClass(); $myVar = new myClass(); echo $myVar(); ``` This can allow for the creation of a single function that has repeatable code inside of the function without having to make a class with a function inside of it having a name, allowing for a shorter syntax when calling the function to run.

« previous php.bugs (#247621) next »