-
Notifications
You must be signed in to change notification settings - Fork 7.9k
User Defined Operator Overloads #7388
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
58a1456
Initial commit of user defined operator overloads
JordanRL c46492d
Added InvalidOperator exception and fixed error handling of operator …
JordanRL 6811080
Started on ZEND_IS_LARGER work
JordanRL 4a84a91
Further implementation of op overloads
JordanRL 6be3d1a
Finished implementation of compare and updated several tests; added s…
JordanRL 5fe2ad1
Fixing various comparison issues and making comparison optional
JordanRL ff79f94
Added bitwise operators and updated several tests
JordanRL 88bd550
Interim commit
JordanRL 0a0d9ec
Resolving merge conflicts
JordanRL 2fa6d98
Improvements to operator overloads and IS_LARGER opcode
JordanRL a2c3f15
Fixing some bugs
JordanRL 80497fc
Updating extensions for changes to objects and ZEND_UNCOMPARABLE
JordanRL 3953f37
Resolving failing tests for operator overloads
JordanRL 6fe6fe2
Fixing extension handling for object compare handler
JordanRL ae584fb
Updating pow test
JordanRL 0ca2ad0
Fixing fallback from __equals to __compareto, and fixing named_params…
JordanRL ce042d1
Resolving some build errors in CI
JordanRL 0139183
Resolving a missed build error in CI
JordanRL 1e3038f
Resolving SIGSEGV
JordanRL f3a44b8
removing ZEND_USER_FUNCTION set
JordanRL 96d72ce
Removing test code
JordanRL cfda296
Removing old comment
JordanRL bbcaf38
Merge branch 'master' into operator-overloads
JordanRL b23f4e7
Trying JIT handling
JordanRL 5ebaed4
Fixing typo in case
JordanRL b042c7d
Another case typo
JordanRL 29ddc0d
Use operator keyword instead of magic methods
bwoebi faab4f2
Merge pull request #3 from bwoebi/operator-overloads
JordanRL 3697f39
Use operator keyword instead of magic methods
bwoebi 52f3f09
Merge pull request #4 from bwoebi/operator-overloads
JordanRL fc3a89c
Updating InvalidOperatorError name
JordanRL 9afc789
Adding more tests for op overloads
JordanRL e044f53
Resolving more memory leaks and segfaults
JordanRL 3bae673
Switched to OperandPosition enum
JordanRL 280ad16
Updates
JordanRL 7e1601b
Fix enum case usage in zend_std_call_op_override
bwoebi 5f500bc
Merge pull request #5 from bwoebi/operator-overloads
JordanRL b0492c4
Adding helper functions for extensions
JordanRL 3d33ddd
Disallow direct method calls to operators
JordanRL 6395654
Reflection support for operator overloads
JordanRL 814d9a2
Merge branch 'master' into operator-overloads
JordanRL 8d1994f
Allowing direct calls via closures
JordanRL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Further implementation of op overloads
- Loading branch information
commit 4a84a91008b7648fc28b9866661e1c3511de402c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
operator overload: add operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __add(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
$return->value = $this->value + $other; | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 3; | ||
|
||
$num1 = 2 + $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj + 3; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(5) | ||
int(6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
operator overload: div operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __div(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
|
||
if ($left) { | ||
$return->value = $this->value / $other; | ||
} else { | ||
$return->value = $other / $this->value; | ||
} | ||
|
||
return $return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: indent |
||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 6; | ||
|
||
$num1 = 12 / $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj / 2; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(2) | ||
int(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--TEST-- | ||
operator overload: div operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __equals(mixed $other): bool | ||
{ | ||
return ($this->value == $other); | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 6; | ||
|
||
$bool1 = 12 == $obj; | ||
|
||
var_dump($bool1); | ||
|
||
$bool2 = $obj == 2; | ||
|
||
var_dump($bool2); | ||
|
||
$bool3 = $obj == 6; | ||
|
||
var_dump($bool3); | ||
|
||
$bool4 = $obj == 6.0; | ||
|
||
var_dump($bool4); | ||
|
||
$bool5 = $obj != 6.0; | ||
|
||
var_dump($bool5); | ||
?> | ||
--EXPECT-- | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
operator overload: mod operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __mod(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
|
||
if ($left) { | ||
$return->value = $this->value % $other; | ||
} else { | ||
$return->value = $other % $this->value; | ||
} | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 2; | ||
|
||
$num1 = 3 % $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj % 4; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(1) | ||
int(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
operator overload: mul operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __mul(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
$return->value = $this->value * $other; | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 3; | ||
|
||
$num1 = 2 * $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj * 3; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(6) | ||
int(9) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
operator overload: no explicit type | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __add($other, bool $left): self | ||
{ | ||
$return = new A(); | ||
$return->value = $this->value + $other; | ||
|
||
return $return; | ||
} | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: A::__add(): Parameter #1 ($other) must explicitly define a type in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--TEST-- | ||
operator overload: unimplemented | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
} | ||
|
||
class B { | ||
public int $value; | ||
|
||
public function __add(int|A $other, bool $left): B | ||
{ | ||
$return = new B(); | ||
if (is_int($other)) { | ||
$return->value = $this->value + $other; | ||
} else { | ||
$return->value = $this->value + $other->value; | ||
} | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj1 = new A(); | ||
$obj1->value = 3; | ||
$obj2 = new B(); | ||
$obj2->value = 2; | ||
|
||
try { | ||
$num1 = $obj1 + 2; | ||
} catch (InvalidOperator) { | ||
echo "OK!".PHP_EOL; | ||
} | ||
|
||
try { | ||
$num2 = 2 + $obj1; | ||
} catch (InvalidOperator) { | ||
echo "STILL OK!".PHP_EOL; | ||
} | ||
|
||
$num3 = $obj1 + $obj2; | ||
|
||
var_dump($num3->value); | ||
|
||
$num4 = $obj2 + $obj1; | ||
|
||
var_dump($num4->value); | ||
|
||
?> | ||
--EXPECT-- | ||
OK! | ||
STILL OK! | ||
int(5) | ||
int(5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
operator overload: pow operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __pow(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
|
||
if ($left) { | ||
$return->value = $this->value ** $other; | ||
} else { | ||
$return->value = $other ** $this->value; | ||
} | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 2; | ||
|
||
$num1 = 5 ** $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj ** 3; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(25) | ||
int(8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
operator overload: sub operator with scalars | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public function __sub(int $other, bool $left): A | ||
{ | ||
$return = new A(); | ||
|
||
if ($left) { | ||
$return->value = $this->value - $other; | ||
} else { | ||
$return->value = $other - $this->value; | ||
} | ||
|
||
return $return; | ||
} | ||
} | ||
|
||
$obj = new A(); | ||
$obj->value = 3; | ||
|
||
$num1 = 2 - $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj - 3; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(-1) | ||
int(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: indent