|
| 1 | +--TEST-- |
| 2 | +Dynamic class constant fetch |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class Foo { |
| 7 | + public const BAR = 'bar'; |
| 8 | +} |
| 9 | + |
| 10 | +function test($code) { |
| 11 | + try { |
| 12 | + var_dump(eval($code)); |
| 13 | + } catch (Throwable $e) { |
| 14 | + echo $e->getMessage(), "\n"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +$const_names = [ |
| 19 | + ['', '"BAR"'], |
| 20 | + ['$bar = "BAR";', '$bar'], |
| 21 | + ['$ba = "BA"; $r = "R";', '$ba . $r'], |
| 22 | + ['', 'strtoupper("bar")'], |
| 23 | + ['', '$barr'], |
| 24 | + ['$bar = "BAR"; $barRef = &$bar;', '$barRef'], |
| 25 | + ['', 'strtolower("CLASS")'], |
| 26 | + ['', '42'], |
| 27 | + ['$bar = 42;', '$bar'], |
| 28 | + ['', '[]'], |
| 29 | + ['$bar = [];', '$bar'], |
| 30 | +]; |
| 31 | + |
| 32 | +foreach ($const_names as [$prolog, $const_name]) { |
| 33 | + test("$prolog return Foo::{{$const_name}};"); |
| 34 | + test("\$foo = 'Foo'; $prolog return \$foo::{{$const_name}};"); |
| 35 | +} |
| 36 | + |
| 37 | +?> |
| 38 | +--EXPECTF-- |
| 39 | +string(3) "bar" |
| 40 | +string(3) "bar" |
| 41 | +string(3) "bar" |
| 42 | +string(3) "bar" |
| 43 | +string(3) "bar" |
| 44 | +string(3) "bar" |
| 45 | +string(3) "bar" |
| 46 | +string(3) "bar" |
| 47 | + |
| 48 | +Warning: Undefined variable $barr in %s : eval()'d code on line %d |
| 49 | +Cannot use value of type null as class constant name |
| 50 | + |
| 51 | +Warning: Undefined variable $barr in %s : eval()'d code on line %d |
| 52 | +Cannot use value of type null as class constant name |
| 53 | +string(3) "bar" |
| 54 | +string(3) "bar" |
| 55 | +string(3) "Foo" |
| 56 | +string(3) "Foo" |
| 57 | +Cannot use value of type int as class constant name |
| 58 | +Cannot use value of type int as class constant name |
| 59 | +Cannot use value of type int as class constant name |
| 60 | +Cannot use value of type int as class constant name |
| 61 | +Cannot use value of type array as class constant name |
| 62 | +Cannot use value of type array as class constant name |
| 63 | +Cannot use value of type array as class constant name |
| 64 | +Cannot use value of type array as class constant name |
0 commit comments