Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

POC https://wiki.php.net/rfc/function-composition#18800

Draft
sgolemon wants to merge 5 commits into
php:masterphp/php-src:masterfrom
sgolemon:compositionssgolemon/php-src:compositionsCopy head branch name to clipboard
Draft

POC https://wiki.php.net/rfc/function-composition#18800
sgolemon wants to merge 5 commits into
php:masterphp/php-src:masterfrom
sgolemon:compositionssgolemon/php-src:compositionsCopy head branch name to clipboard

Conversation

@sgolemon
Copy link
Copy Markdown
Member

@sgolemon sgolemon commented Jun 7, 2025

No description provided.

@sgolemon
Copy link
Copy Markdown
Member Author

sgolemon commented Jun 7, 2025

Needs more testing, and as @Crell already noted on discord, there's a gap for:

class A {
    public function __invoke($x) { return $x; }
}

$cb = (new A) + (new A);

As these have no class level overloading of the add operator.

Comment thread Zend/zend_closures.c
closure_handlers.get_debug_info = zend_closure_get_debug_info;
closure_handlers.get_closure = zend_closure_get_closure;
closure_handlers.get_gc = zend_closure_get_gc;
closure_handlers.do_operation = zend_closure_do_operation;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! That's not at all how I would have thought to do it.

If I follow, this is implemented as an operator override on closures, rather than special casing the operator itself, yes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. When the engine encounts a binary op (like addition) involving one of more objects, it checks them for do_operation overloads (with priority to the lhs) and asks the object to perform the addition itself. The first one which reports SUCCESS and populates result wins. So if you added (gmp) + (closure), then GMP would try to do the addition, have no idea what to make of a closure, and fail, and the closure would do the same (not knowing what to do with a gmp), and you'd wind up with an error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, an explicit engine level overload of the binary-op helper would allow us to cover situations like this one.

e.g.

diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c
index 140734d1b96..b9f9629c616 100644
--- a/Zend/zend_operators.c
+++ b/Zend/zend_operators.c
@@ -1128,6 +1128,11 @@ static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *resul
 
        ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
 
+       if (((Z_TYPE_P(op1) == IS_OBJECT) || (Z_TYPE_P(op2) == IS_OBJECT)) &&
+         (zend_composed_callable_new_from_pair(result, op1, op2) == SUCCESS)) {
+               return SUCCESS;
+       }
+
        zval op1_copy, op2_copy;
        if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
                        || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heck, could even cover the $cb = 'strtolower' + 'strrev'; kind of cases that way, if you wanted to go for maximum magic (I don't).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. When the engine encounts a binary op (like addition) involving one of more objects, it checks them for do_operation overloads (with priority to the lhs) and asks the object to perform the addition itself. The first one which reports SUCCESS and populates result wins. So if you added (gmp) + (closure), then GMP would try to do the addition, have no idea what to make of a closure, and fail, and the closure would do the same (not knowing what to do with a gmp), and you'd wind up with an error.

Our operator overloading mechanism is far from robust, and GMP used to throw its own exception and not delegate properly to the fallback.
Moreover, behaviour with other values might still be somewhat broken, this really needs tests with every possible scalar and null as the LHS and RHS of +

@Crell
Copy link
Copy Markdown
Contributor

Crell commented Jun 8, 2025

Hm, I can't even get this to compile, even with clean. Is there a file missing?

/usr/bin/ld: Zend/zend_closures.o: in function `zend_closure_do_operation':
/home/crell/Projects/Forks/php-src/Zend/zend_closures.c:714: undefined reference to `zend_composed_callable_new_from_pair'
/usr/bin/ld: Zend/zend_interfaces.o: in function `zend_register_interfaces':
/home/crell/Projects/Forks/php-src/Zend/zend_interfaces.c:681: undefined reference to `zend_register_composed_callable'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0x8): undefined reference to `zim_ComposedCallable___construct'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0x38): undefined reference to `zim_ComposedCallable___invoke'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0x68): undefined reference to `zim_ComposedCallable___debugInfo'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0x98): undefined reference to `zim_ComposedCallable_append'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0xc8): undefined reference to `zim_ComposedCallable_insert'
/usr/bin/ld: Zend/zend_interfaces.o:(.data.rel.ro+0xf8): undefined reference to `zim_ComposedCallable_prepend'

@Girgias
Copy link
Copy Markdown
Member

Girgias commented Jun 8, 2025

I don't really understand why we need a new CE? Why can't the composition of two Closures not just return a new Closure?

@sgolemon
Copy link
Copy Markdown
Member Author

sgolemon commented Jun 8, 2025

Hm, I can't even get this to compile, even with clean. Is there a file missing?

/usr/bin/ld: Zend/zend_closures.o: in function `zend_closure_do_operation':
/home/crell/Projects/Forks/php-src/Zend/zend_closures.c:714: undefined reference to `zend_composed_callable_new_from_pair'

You need to rebuild and rerun configure since there's a new build target.

./buildconf --force
./configure
make

@sgolemon
Copy link
Copy Markdown
Member Author

sgolemon commented Jun 8, 2025

I don't really understand why we need a new CE? Why can't the composition of two Closures not just return a new Closure?

Could. I just chose this route. I would say that it allows things like if (($cb instanceof \ComposedCallable) && $cb->hasStage($otherCallable)) ... which... might be neat...

@sgolemon
Copy link
Copy Markdown
Member Author

@Crell Brought your tests in and fixed a bug I must have introduced right before my original push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.