Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2012 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3132746 to your computer and use it in GitHub Desktop.
Save anonymous/3132746 to your computer and use it in GitHub Desktop.
Operator-overload in PHP
diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c
index 93dca94..1e8358c 100644
--- a/Zend/zend_operators.c
+++ b/Zend/zend_operators.c
@@ -23,6 +23,7 @@
#include "zend.h"
#include "zend_operators.h"
+#include "zend_interfaces.h"
#include "zend_variables.h"
#include "zend_globals.h"
#include "zend_list.h"
@@ -794,6 +795,13 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ *
zval op1_copy, op2_copy;
int converted = 0;
+ if (Z_TYPE_P(op1) == IS_OBJECT && zend_hash_exists(&Z_OBJCE_P(op1)->function_table, "__add", strlen("__add") + 1)) {
+ zval* tmp;
+ zend_call_method_with_1_params(&op1, NULL, NULL, "__add", &tmp, op2);
+ ZVAL_ZVAL(result, tmp, 0, 1);
+ return SUCCESS;
+ }
+
while (1) {
switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
case TYPE_PAIR(IS_LONG, IS_LONG): {
@@ -856,6 +864,13 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ *
zval op1_copy, op2_copy;
int converted = 0;
+ if (Z_TYPE_P(op1) == IS_OBJECT && zend_hash_exists(&Z_OBJCE_P(op1)->function_table, "__sub", strlen("__sub") + 1)) {
+ zval* tmp;
+ zend_call_method_with_1_params(&op1, NULL, NULL, "__sub", &tmp, op2);
+ ZVAL_ZVAL(result, tmp, 0, 1);
+ return SUCCESS;
+ }
+
while (1) {
switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
case TYPE_PAIR(IS_LONG, IS_LONG): {
@@ -903,6 +918,13 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ *
zval op1_copy, op2_copy;
int converted = 0;
+ if (Z_TYPE_P(op1) == IS_OBJECT && zend_hash_exists(&Z_OBJCE_P(op1)->function_table, "__mul", strlen("__mul") + 1)) {
+ zval* tmp;
+ zend_call_method_with_1_params(&op1, NULL, NULL, "__mul", &tmp, op2);
+ ZVAL_ZVAL(result, tmp, 0, 1);
+ return SUCCESS;
+ }
+
while (1) {
switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
case TYPE_PAIR(IS_LONG, IS_LONG): {
@@ -944,6 +966,13 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ *
zval op1_copy, op2_copy;
int converted = 0;
+ if (Z_TYPE_P(op1) == IS_OBJECT && zend_hash_exists(&Z_OBJCE_P(op1)->function_table, "__div", strlen("__div") + 1)) {
+ zval* tmp;
+ zend_call_method_with_1_params(&op1, NULL, NULL, "__div", &tmp, op2);
+ ZVAL_ZVAL(result, tmp, 0, 1);
+ return SUCCESS;
+ }
+
while (1) {
switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
case TYPE_PAIR(IS_LONG, IS_LONG):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment