php / php-src

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

Anonymous Functions/Methods Inside Of Classes #15622

Open trymeouteh opened 2 months ago

trymeouteh commented 2 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 2 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 2 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 2 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 2 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?