-
Notifications
You must be signed in to change notification settings - Fork 110
Add support for anonymous classes #273
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
drdzyk
wants to merge
1
commit into
master
Choose a base branch
from
seismont/add-support-for-anonymous-classes
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
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
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,13 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| function test_set_to_variable() { | ||
| $a = new class { | ||
| function say_foo() { | ||
| echo "foo"; | ||
| } | ||
| }; | ||
| $a->say_foo(); | ||
| } | ||
|
|
||
| test_set_to_variable(); |
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,20 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| interface I { | ||
| function say_foo(); | ||
| } | ||
|
|
||
| function call_say_foo(I $i) { | ||
| $i->say_foo(); | ||
| } | ||
|
|
||
| function test_pass_to_function() { | ||
| call_say_foo(new class implements I { | ||
| function say_foo() { | ||
| echo "foo"; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test_pass_to_function(); |
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,21 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| function test_one_ctor_arg() { | ||
| new class(77) { | ||
| function __construct($num) { | ||
| echo $num; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function test_two_ctor_arg() { | ||
| new class("foo", "bar") { | ||
| function __construct($s1, $s2) { | ||
| echo $s1, $s2; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| test_one_ctor_arg(); | ||
| test_two_ctor_arg(); |
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,32 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| // example from https://www.php.net/manual/en/language.oop5.anonymous.php | ||
| class Outer { | ||
| private $prop = 1; | ||
| protected $prop2 = 2; | ||
|
|
||
| protected function func1() { | ||
| return 3; | ||
| } | ||
|
|
||
| public function func2() { | ||
| return new class($this->prop) extends Outer { | ||
| private $prop3; | ||
|
|
||
| public function __construct($prop) { | ||
| $this->prop3 = $prop; | ||
| } | ||
|
|
||
| public function func3() { | ||
| return $this->prop2 + $this->prop3 + $this->func1(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function test_access_to_outer_class() { | ||
| echo (new Outer)->func2()->func3(); | ||
| } | ||
|
|
||
| test_access_to_outer_class(); |
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,24 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| class Base { | ||
| public function sayHello() { | ||
| echo 'Hello '; | ||
| } | ||
| } | ||
|
|
||
| trait SayWorld { | ||
| public function sayHello() { | ||
| parent::sayHello(); | ||
| echo 'World!'; | ||
| } | ||
| } | ||
|
|
||
| function test_extend_traits() { | ||
| $obj = new class extends Base { | ||
| use SayWorld; | ||
| }; | ||
| $obj->sayHello(); | ||
| } | ||
|
|
||
| test_extend_traits(); |
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,17 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| function get_anon_class() { | ||
| return new class {}; | ||
| } | ||
|
|
||
| function get_another_anon_class() { | ||
| return new class {}; | ||
| } | ||
|
|
||
| function test_anon_classes_equality() { | ||
| echo (get_class(get_anon_class()) === get_class(get_anon_class())); | ||
| echo (get_class(get_anon_class()) === get_class(get_another_anon_class())); | ||
| } | ||
|
|
||
| test_anon_classes_equality(); |
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,11 @@ | ||
| @kphp_should_fail | ||
| /pass int to argument \$num of anon_class.*::__construct\nbut it's declared as @param string/ | ||
| <?php | ||
|
|
||
| function test_constructor_type_mismatch() { | ||
| new class(42) { | ||
| function __construct(string $num) {} | ||
| }; | ||
| } | ||
|
|
||
| test_constructor_type_mismatch(); | ||
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.
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.
Full log of compiler error: