-
Notifications
You must be signed in to change notification settings - Fork 866
Add missing ext/operator documentation #4669
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
Open
jb-lopez
wants to merge
3
commits into
php:master
Choose a base branch
from
jb-lopez:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- $Revision$ --> | ||
| <book xml:id="book.operator" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
| <?phpdoc extension-membership="pecl" ?> | ||
| <title>Operator Overloading for Objects</title> | ||
| <titleabbrev>Operator Overloading</titleabbrev> | ||
|
|
||
| <!-- {{{ preface --> | ||
| <preface xml:id="intro.operator"> | ||
| &reftitle.intro; | ||
| <simpara> | ||
| This extension defines and implements operator overloading for objects. | ||
| It allows defining how an object reacts when an operator is used on it. | ||
| </simpara> | ||
| <simpara> | ||
| One example of this is creating a collection type object that has the addition | ||
| operator overloaded to allow adding elements to the collection or adding two | ||
| collections together. | ||
| </simpara> | ||
| <simpara> | ||
| Another example is creating an enhanced string class that has the multiplication | ||
| operator overloaded to allow repeating the string a certain number of times. | ||
| </simpara> | ||
| <section> | ||
| </section> | ||
| </preface> | ||
| <!-- }}} --> | ||
| &reference.operator.setup; | ||
| &reference.operator.overloading; | ||
| </book> |
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,340 @@ | ||||||||||||||||||||||
| <?xml version="1.0" encoding="utf-8"?> | ||||||||||||||||||||||
| <!-- $Revision$ --> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| <chapter xml:id="operator.overloading" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||||||||||||||||||||||
| <title>Operator Overloading Magic Methods</title> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| The operator overloading extension allows you to define how an object reacts to operators. This is done by implementing the following magic methods: | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Arithmetic operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__add($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__sub($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__mul($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__div($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__mod($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__pow($b)</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Assignment operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__assign($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_add($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_sub($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_mul($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_div($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_mod($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_pow($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_bw_and($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_bw_or($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_bw_xor($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_sl($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_sr($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__assign_concat($b)</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Bitwise operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__bw_and($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__bw_or($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__bw_xor($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__bw_not()</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__sl($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__sr($b)</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Comparison operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__is_equal($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_not_equal($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_identical($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_not_identical($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_smaller($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_smaller_or_equal($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_greater($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__is_greater_or_equal($b)</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__spaceship($b)</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Incrementing and decrementing operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__pre_inc()</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__post_inc()</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__pre_dec()</literal></member> | ||||||||||||||||||||||
| <member><literal>$a::__post_dec()</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| String operators: | ||||||||||||||||||||||
| <simplelist> | ||||||||||||||||||||||
| <member><literal>$a::__concat($b)</literal></member> | ||||||||||||||||||||||
| </simplelist> | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
| <section> | ||||||||||||||||||||||
| <title>Operator Overloading Examples</title> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| The following is the class that is used in the testing of the operator overloading extension. | ||||||||||||||||||||||
| It overloads all of the possible operators that can be overloaded for testing. | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
|
Comment on lines
+80
to
+83
Contributor
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.
Suggested change
|
||||||||||||||||||||||
| <example xml:id="operator.overloading.complete-class"> | ||||||||||||||||||||||
| <title>Complete class for operator overloading</title> | ||||||||||||||||||||||
| <programlisting role="php"> | ||||||||||||||||||||||
| <![CDATA[ | ||||||||||||||||||||||
| <?php | ||||||||||||||||||||||
| class OperatorOverloading { | ||||||||||||||||||||||
| protected mixed $value; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Standard magic methods | ||||||||||||||||||||||
| public function __get(string $name) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __set(string $name, mixed $value) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->value = $value; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __construct(mixed $init = null) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->value = $init; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Arithmetic Operators | ||||||||||||||||||||||
| public function __add(mixed $val): int|float | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value + $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __div(mixed $val): int|float | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value / $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __mod(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value % $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __mul(mixed $val): int|float | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value * $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __pow(mixed $val): int|float | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value ** $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __sub(mixed $val): int|float | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value - $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Assignment Operators | ||||||||||||||||||||||
| public function __assign(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value = $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_add(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value += $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_bw_and(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value &= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_bw_or(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value |= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_concat(mixed $val): string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value .= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_div(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value /= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_mod(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value %= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_mul(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value *= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_pow(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value **= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_sl(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value <<= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_sr(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value >>= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __assign_sub(mixed $val): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value -= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Bitwise Operators | ||||||||||||||||||||||
| public function __bw_and(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value & $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __bw_not(): int|string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return ~$this->value; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __bw_or(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value | $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __bw_xor(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value ^ $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __sl(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value << $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __sr(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value >> $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Comparison Operators | ||||||||||||||||||||||
| public function __is_equal(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value == $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_greater(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value > $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_greater_or_equal(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value >= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_identical(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value === $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_not_equal(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value != $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_not_identical(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value !== $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_smaller(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value < $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __is_smaller_or_equal(mixed $val): bool | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value <= $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __spaceship(mixed $val): int | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value <=> $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region Incrementing/Decrementing Operators | ||||||||||||||||||||||
| public function __post_dec(): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value--; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __post_inc(): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value++; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __pre_dec(): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return --$this->value; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function __pre_inc(): mixed | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return ++$this->value; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //region String Operators | ||||||||||||||||||||||
| public function __concat(mixed $val): string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->value . $val; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| //endregion | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ]]> | ||||||||||||||||||||||
| </programlisting> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| Using the above class, you can overload the operators as follows: | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
|
Comment on lines
+315
to
+317
Contributor
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.
Suggested change
|
||||||||||||||||||||||
| <programlisting role="php"> | ||||||||||||||||||||||
| <![CDATA[ | ||||||||||||||||||||||
| <?php | ||||||||||||||||||||||
| $a = new OperatorOverloading(5); | ||||||||||||||||||||||
| var_dump($a + 10); | ||||||||||||||||||||||
| var_dump($a - 10); | ||||||||||||||||||||||
| $b = new OperatorOverloading("Hello"); | ||||||||||||||||||||||
| var_dump($b . " World"); | ||||||||||||||||||||||
| ]]> | ||||||||||||||||||||||
| </programlisting> | ||||||||||||||||||||||
| <para> | ||||||||||||||||||||||
| The above code will output: | ||||||||||||||||||||||
| </para> | ||||||||||||||||||||||
|
Comment on lines
+328
to
+330
Contributor
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.
Suggested change
|
||||||||||||||||||||||
| <screen> | ||||||||||||||||||||||
| <![CDATA[ | ||||||||||||||||||||||
| int(15) | ||||||||||||||||||||||
| int(-5) | ||||||||||||||||||||||
| string(11) "Hello World" | ||||||||||||||||||||||
| ]]> | ||||||||||||||||||||||
| </screen> | ||||||||||||||||||||||
| </example> | ||||||||||||||||||||||
| </section> | ||||||||||||||||||||||
| </chapter> | ||||||||||||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.