-
Notifications
You must be signed in to change notification settings - Fork 8k
[RFC] Implement dynamic class const fetch #9793
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--TEST-- | ||
Dynamic class constant fetch | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public const BAR = 'bar'; | ||
} | ||
|
||
function test($code) { | ||
try { | ||
var_dump(eval($code)); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
$const_names = [ | ||
['', '"BAR"'], | ||
['$bar = "BAR";', '$bar'], | ||
['$ba = "BA"; $r = "R";', '$ba . $r'], | ||
['', 'strtoupper("bar")'], | ||
['', '$barr'], | ||
['$bar = "BAR"; $barRef = &$bar;', '$barRef'], | ||
['', 'strtolower("CLASS")'], | ||
['', '42'], | ||
['$bar = 42;', '$bar'], | ||
['', '[]'], | ||
['$bar = [];', '$bar'], | ||
]; | ||
|
||
foreach ($const_names as [$prolog, $const_name]) { | ||
test("$prolog return Foo::{{$const_name}};"); | ||
test("\$foo = 'Foo'; $prolog return \$foo::{{$const_name}};"); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "bar" | ||
|
||
Warning: Undefined variable $barr in %s : eval()'d code on line %d | ||
Cannot use value of type null as class constant name | ||
|
||
Warning: Undefined variable $barr in %s : eval()'d code on line %d | ||
Cannot use value of type null as class constant name | ||
string(3) "bar" | ||
string(3) "bar" | ||
string(3) "Foo" | ||
string(3) "Foo" | ||
Cannot use value of type int as class constant name | ||
Cannot use value of type int as class constant name | ||
Cannot use value of type int as class constant name | ||
Cannot use value of type int as class constant name | ||
Cannot use value of type array as class constant name | ||
Cannot use value of type array as class constant name | ||
Cannot use value of type array as class constant name | ||
Cannot use value of type array as class constant name |
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,61 @@ | ||
--TEST-- | ||
Dynamic class constant fetch | ||
--FILE-- | ||
<?php | ||
|
||
class FooParent { | ||
public const BAR = 'bar'; | ||
public const BAZ = 'baz'; | ||
} | ||
|
||
class Foo extends FooParent { | ||
public const BAZ = 'baz child'; | ||
} | ||
|
||
class BarParent { | ||
public const BAR = 'bar 2'; | ||
public const BAZ = 'baz 2'; | ||
} | ||
|
||
class Bar extends BarParent { | ||
public const BAZ = 'baz 2 child'; | ||
} | ||
|
||
function test($const) { | ||
echo Foo::{$const}, "\n"; | ||
$foo = 'Foo'; | ||
echo $foo::{$const}, "\n"; | ||
} | ||
|
||
test('BAR'); | ||
test('BAZ'); | ||
|
||
$c = function ($const) { | ||
echo self::{$const}, "\n"; | ||
echo static::{$const}, "\n"; | ||
echo parent::{$const}, "\n"; | ||
}; | ||
|
||
$c->bindTo(null, Foo::class)('BAR'); | ||
$c->bindTo(null, Bar::class)('BAZ'); | ||
$c->bindTo(null, Foo::class)('class'); | ||
$c->bindTo(null, Bar::class)('class'); | ||
|
||
?> | ||
--EXPECT-- | ||
bar | ||
bar | ||
baz child | ||
baz child | ||
bar | ||
bar | ||
bar | ||
baz 2 child | ||
baz 2 child | ||
baz 2 | ||
Foo | ||
Foo | ||
FooParent | ||
Bar | ||
Bar | ||
BarParent |
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 @@ | ||
--TEST-- | ||
Dynamic class constant fetch in constant expressions | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public const BAR = 'bar'; | ||
public const BA = 'BA'; | ||
public const R = 'R'; | ||
public const CLASS_ = 'class'; | ||
public const A = self::{'BAR'}; | ||
public const B = self::{'BA' . 'R'}; | ||
public const C = self::{self::BA . self::R}; | ||
} | ||
|
||
var_dump(Foo::A); | ||
var_dump(Foo::B); | ||
var_dump(Foo::C); | ||
|
||
?> | ||
--EXPECT-- | ||
string(3) "bar" | ||
string(3) "bar" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
Dynamic class constant fetch DIM order | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
public const FOO = 'Foo'; | ||
} | ||
|
||
function foo() { | ||
echo "foo()\n"; | ||
return 'FOO'; | ||
} | ||
|
||
function bar() { | ||
echo "bar()\n"; | ||
return 'BAR'; | ||
} | ||
|
||
function test($c) { | ||
try { | ||
echo $c(), "\n"; | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
test(fn() => Foo::{foo()}::{bar()}); | ||
test(fn() => Foo::{bar()}::{foo()}); | ||
|
||
?> | ||
--EXPECT-- | ||
foo() | ||
bar() | ||
Undefined constant Foo::BAR | ||
bar() | ||
Undefined constant Foo::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
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.
Uh oh!
There was an error while loading. Please reload this page.