-
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
Changes from all commits
58a1456
c46492d
6811080
4a84a91
6be3d1a
5fe2ad1
ff79f94
88bd550
0a0d9ec
2fa6d98
a2c3f15
80497fc
3953f37
6fe6fe2
ae584fb
0ca2ad0
ce042d1
0139183
1e3038f
f3a44b8
96d72ce
cfda296
bbcaf38
b23f4e7
5ebaed4
b042c7d
29ddc0d
faab4f2
3697f39
52f3f09
fc3a89c
9afc789
e044f53
3bae673
280ad16
7e1601b
5f500bc
b0492c4
3d33ddd
6395654
814d9a2
8d1994f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 operator +(int $other, OperandPosition $left): A | ||
{ | ||
$return = new A(); | ||
$return->value = $this->value + $other; | ||
|
||
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 = 3; | ||
|
||
$num1 = 2 + $obj; | ||
|
||
var_dump($num1->value); | ||
|
||
$num2 = $obj + 3; | ||
|
||
var_dump($num2->value); | ||
?> | ||
--EXPECT-- | ||
int(5) | ||
int(6) |
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 $value; | ||
|
||
public operator /(int|float $other, OperandPosition $left): A | ||
{ | ||
$return = new A(); | ||
|
||
if ($left == OperandPosition::LeftSide) { | ||
$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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--TEST-- | ||
operator overload: equals fallback to comparison operator | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public int $value; | ||
|
||
public operator <=>(mixed $other): int | ||
{ | ||
return ($this->value <=> $other); | ||
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 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. Was being confused because the file view was showing them misalligned, but it's because you're mixing tabs and spaces, PHPT files should be space indented. |
||
} | ||
} | ||
|
||
$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) |
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.
https://stackoverflow.com/a/7335487/1320374 😉
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.
Thanks @iluuu1994. I was going to take care of this before I opened the PR from draft.
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.
Np, I thought maybe you didn't know about global gitignores.
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.
I actually didn't, so the comment was definitely helpful. :)