-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Arithmetic magic methods #3735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arithmetic magic methods #3735
Conversation
Currently the summation operation with arrays works like concatenation and this is causing the errors. I will fix it. |
PHP does support this via the |
@nikic Thanks a lot! You just solved a nightmare for me, this was essential to my project. I didn't realize the existence of this object handler. Not wanting to take up too much of your time, but then the implementation would be something like: static zend_object_handlers my_handler;
ZEND_MINIT_FUNCTION(MyProject)
{
memcpy(&myproject_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
myproject_object_handlers.do_operation = my_do_operation;
} Is this correct? Thanks again |
@henrique-borba Right, and you will also have to assign your custom object handlers inside your create_object class handler (or create it if you don't have one yet). You can take a look at ext/gmp for an example of an extension that uses do_operation. |
@nikic does Thanks. |
@weltling I think there are some opcache optimizations that may swap operand order. We can drop them for ZEND_MUL at least. |
I've removed the opcache optimization that I'm aware of in 1165a90. Maybe there are others. |
As we already have the do_operation mechanism, I'm going to close this PR. Feel free to ping me if you encounter any problems with it. |
Hi @nikic, Thanks a lot again. I'm happy to see you cared about OPCache swaps. I just realized I must opened an RFC por this kind of change, anyway, I don't think I will do it as your guidance was enough. I realize your OPCache change would only affect PHP next major release (Am I wrong?). Is it possible to detect operand swap before it happens? I would like to make arithmetic operations available for PHP 7.X. I could for example, swap the operands before OPCache swap so theier order will be as expected? Excuse my english, I refuse using Google Translator haha. Anyway, thanks a lot. @nikic I'm a big fan of your work, just having your attention is a win. See you. |
I'm currently developing an extension for PHP for matrix calculations but PHP does not have the option of intercepting arithmetic operations using objects. I then decided to update the arithmetic functions in
zend_operators.c
to work with objects.Use Case
These feature is especially relevant for Math and Linear Algebra libraries. Currently, if I want to create a library to multiply and add arrays, it would have to be developed in a way that calls become repetitive and often make a mess:
This could be written like:
Some people however could use these methods to add an object to a collection in a different (maybe bizarre) way:
PS: Only an example, I personally would stay with
$people::add
in this case.