Repository navigation
Anonymous Classes #1118
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
Anonymous Classes #1118
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e62554a
anon class
krakjoe dd7c61f
tests for anons
krakjoe 39e3528
fix nesting error
krakjoe e90e66e
moar tests passing
krakjoe 86536ef
...
krakjoe fd98e20
pass all tests
krakjoe b0c6856
...
krakjoe 54d3370
drop extends use parent from name node
krakjoe 84e5225
...
krakjoe ff02ee3
remove unused global
krakjoe 4b74a9a
add reflection method
krakjoe 6e8e84d
...
krakjoe 95a0f5c
...
krakjoe fa5fa07
cleanup warnings
krakjoe 0125d22
move constructor args
krakjoe 84ec180
update tests for clarity
krakjoe e201d36
Merge branch 'anon' of https://github.com/krakjoe/php-src into krakjo…
krakjoe e1125d6
Added test for ReflectionClass::isAnonymous()
3b8a5b1
Merge pull request #9 from philsturgeon/anon
krakjoe dc5dc2c
testing static access for methods and properties in anon classes
b9dd293
Added test showing traits in anon classes.
62457a4
Merge branch 'anon' of https://github.com/krakjoe/php-src into krakjo…
krakjoe 2dbd04d
deny serialization
krakjoe 3254e98
include interfaces header
krakjoe 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --TEST-- | ||
| declare bare anonymous class | ||
| --FILE-- | ||
| <?php | ||
| var_dump(new class{}); | ||
| --EXPECTF-- | ||
| object(class@%s)#%d (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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| declare anonymous class extending another | ||
| --FILE-- | ||
| <?php | ||
| class A{} | ||
|
|
||
| interface B{ | ||
| public function method(); | ||
| } | ||
|
|
||
| $a = new class extends A implements B { | ||
| public function method(){ | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| var_dump($a instanceof A, $a instanceof B); | ||
| --EXPECTF-- | ||
| bool(true) | ||
| bool(true) | ||
|
|
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,54 @@ | ||
| --TEST-- | ||
| reusing anonymous classes | ||
| --FILE-- | ||
| <?php | ||
| while (@$i++<10) { | ||
| var_dump(new class($i) { | ||
|
|
||
| public function __construct($i) { | ||
| $this->i = $i; | ||
| } | ||
| }); | ||
| } | ||
| --EXPECTF-- | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(1) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(2) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(3) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(4) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(5) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(6) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(7) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(8) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(9) | ||
| } | ||
| object(class@%s)#1 (1) { | ||
| ["i"]=> | ||
| int(10) | ||
| } | ||
|
|
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 @@ | ||
| --TEST-- | ||
| testing anonymous inheritance | ||
| --FILE-- | ||
| <?php | ||
| class Outer { | ||
| protected $data; | ||
|
|
||
| public function __construct($data) { | ||
| $this->data = $data; | ||
| } | ||
|
|
||
| public function getArrayAccess() { | ||
| /* create a proxy object implementing array access */ | ||
| return new class($this->data) extends Outer implements ArrayAccess { | ||
| public function offsetGet($offset) { return $this->data[$offset]; } | ||
| public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); } | ||
| public function offsetUnset($offset) { unset($this->data[$offset]); } | ||
| public function offsetExists($offset) { return isset($this->data[$offset]); } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| $outer = new Outer(array( | ||
| rand(1, 100) | ||
| )); | ||
|
|
||
| /* not null because inheritance */ | ||
| var_dump($outer->getArrayAccess()[0]); | ||
| --EXPECTF-- | ||
| int(%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,36 @@ | ||
| --TEST-- | ||
| testing reusing anons that implement an interface | ||
| --FILE-- | ||
| <?php | ||
| class Outer { | ||
| protected $data; | ||
|
|
||
| public function __construct(&$data) { | ||
| /* array access will be implemented by the time we get to here */ | ||
| $this->data = &$data; | ||
| } | ||
|
|
||
| public function getArrayAccess() { | ||
| /* create a child object implementing array access */ | ||
| /* this grants you access to protected methods and members */ | ||
| return new class($this->data) implements ArrayAccess { | ||
| public function offsetGet($offset) { return $this->data[$offset]; } | ||
| public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); } | ||
| public function offsetUnset($offset) { unset($this->data[$offset]); } | ||
| public function offsetExists($offset) { return isset($this->data[$offset]); } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| $data = array( | ||
| rand(1, 100), | ||
| rand(2, 200) | ||
| ); | ||
|
|
||
| $outer = new Outer($data); | ||
| $proxy = $outer->getArrayAccess(); | ||
|
|
||
| /* null because no inheritance, so no access to protected member */ | ||
| var_dump(@$outer->getArrayAccess()[0]); | ||
| --EXPECT-- | ||
| NULL |
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,15 @@ | ||
| --TEST-- | ||
| testing anon classes inside namespaces | ||
| --FILE-- | ||
| <?php | ||
| namespace lone { | ||
| $hello = new class{} ; | ||
| } | ||
|
|
||
| namespace { | ||
| var_dump ($hello); | ||
| } | ||
| --EXPECTF-- | ||
| object(lone\class@%s)#1 (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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --TEST-- | ||
| testing anon classes in functions outside of classes in namespaces | ||
| --FILE-- | ||
| <?php | ||
| namespace lone { | ||
| function my_factory() { | ||
| return new class{}; | ||
| } | ||
|
|
||
| class Outer { | ||
|
|
||
| public function __construct() { | ||
| var_dump( | ||
| my_factory()); | ||
| } | ||
| } | ||
|
|
||
| new Outer(); | ||
| } | ||
| --EXPECTF-- | ||
| object(lone\class@%s)#2 (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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --TEST-- | ||
| testing static access for methods and properties in anon classes | ||
| --FILE-- | ||
| <?php | ||
| $anonClass = new class("cats", "dogs") { | ||
| public static $foo; | ||
| private static $bar; | ||
|
|
||
| public function __construct($foo, $bar) { | ||
| static::$foo = $foo; | ||
| static::$bar = $bar; | ||
| } | ||
|
|
||
| public static function getBar() { | ||
| return static::$bar; | ||
| } | ||
| }; | ||
|
|
||
| var_dump($anonClass::$foo); | ||
| var_dump($anonClass::getBar()); | ||
| --EXPECT-- | ||
| string(4) "cats" | ||
| string(4) "dogs" |
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,18 @@ | ||
| --TEST-- | ||
| testing traits in anon classes | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| trait Foo { | ||
| public function someMethod() { | ||
| return "bar"; | ||
| } | ||
| } | ||
|
|
||
| $anonClass = new class { | ||
| use Foo; | ||
| }; | ||
|
|
||
| var_dump($anonClass->someMethod()); | ||
| --EXPECT-- | ||
| string(3) "bar" |
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.
The changes in this file seem unrelated.