php / php-src

The PHP Interpreter
https://www.php.net
Other
38.34k stars 7.77k forks source link

Anonymous Functions/Methods Inside Of Classes #15622

Open trymeouteh opened 3 months ago

trymeouteh commented 3 months ago

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.

jimwins commented 3 months ago

How is this different from the magic __invoke() method?

https://www.php.net/manual/en/language.oop5.magic.php#object.invoke

trymeouteh commented 3 months ago

How is this different from the magic __invoke() method?

https://www.php.net/manual/en/language.oop5.magic.php#object.invoke

I never knew of the __invoke() method. Why can't the __invoke() method be set to a static method to allow for calling the method without creating an object?

<?php

class myClass {
    private static function add($a, $b) {
        return $a + $b;
    }

    private static function subtract($a, $b) {
        return $a - $b;
    }

    public function __invoke() {
        $add = self::add(2, 2);
        $subtract = self::subtract(9, 3);

        return $add . PHP_EOL . $subtract . PHP_EOL;
    }
}

echo myClass();

$myVar = new myClass();
echo $myVar();
cmb69 commented 3 months ago

Besides that static classes may not be everybodies favorite thing to have (see e.g. https://wiki.php.net/rfc/static_class, which had been declined), that would cause an issue with symbol detection. As it's now, during compilation it is certain that myClass() refers to a function, not a class.

If you still want to see the ability to call a static class method directly on the class, please pursue the RFC process.

MorganLOCode commented 3 months ago

Isn't the example basically


function myClass() {
    static $add = fn($a, $b) => $a + $b;

    static $subtract = fn($a, $b) => $a - $b;

    $sum = $add(2, 2);
    $difference = $subtract(9, 3);

    return $sum . PHP_EOL . $difference . PHP_EOL;
}

echo myClass();

Considering that such a class can have only one such "anonymous" static method, how is it different from a function?

github-actions[bot] commented 5 days ago

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.