forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp7.parser
More file actions
132 lines (112 loc) · 2.49 KB
/
php7.parser
File metadata and controls
132 lines (112 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Test PHP7 syntax
--PASS:Return type declarations--
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
--PASS--
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
--PASS:Spaceship operator--
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
--PASS:Constant arrays using define()--
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
--PASS:Anonymous classes--
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
var_dump($app->getLogger());
--PASS:Unicode codepoint escape syntax--
echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";
--PASS:Closure::call()--
class A {private $x = 1;}
// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // intermediate closure
echo $getX();
// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);
--PASS:Group use declarations--
// Pre PHP 7 code
use some\ns\ClassA;
use some\ns\ClassB;
use some\ns\ClassC as C;
use function some\ns\fn_a;
use function some\ns\fn_b;
use function some\ns\fn_c;
use const some\ns\ConstA;
use const some\ns\ConstB;
use const some\ns\ConstC;
// PHP 7+ code
use some\ns\{ClassA, ClassB, ClassC as C};
use function some\ns\{fn_a, fn_b, fn_c};
use const some\ns\{ConstA, ConstB, ConstC};
--PASS:Generator Return Expressions--
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
}
echo $gen->getReturn(), PHP_EOL;
--PASS:Generator delegation--
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
yield 3;
yield 4;
}
foreach (gen() as $val)
{
echo $val, PHP_EOL;
}
--PASS--
(clone $foo)->bar();
class foo { static $bar = 'baz'; }
var_dump('foo'::$bar);