-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathconstants.phpt
231 lines (186 loc) · 6.93 KB
/
constants.phpt
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
--TEST--
PHP Spec test generated from ./constants/constants.php
--FILE--
<?php
/*
+-------------------------------------------------------------+
| Copyright (c) 2014 Facebook, Inc. (http://www.facebook.com) |
+-------------------------------------------------------------+
*/
error_reporting(-1);
function trace($name, $value)
{
$r = define($name, $value);
echo "define $name " . ($r ? "succeeded" : "failed");
if (defined($name))
echo "; value is >" . constant($name) . "<\n";
else
echo "; not defined\n";
}
///*
// define some constants with simple (single-token) scalar initial values
trace("STATUS1", TRUE);
trace("MIN", 10);
trace("MAX", 20);
trace("MY_PI", 3.1415926);
trace("MY_COLOR", "red");
trace("C1", NULL);
//*/
///*
// try to define some constants with multiple-token scalar initial values
// involving literals only
trace("CON1", 5 + 10.1 * 3);// succeeded;
// involving variables and other non-basic operators
function getValue() { return 250; }
$v1 = 10;
trace("CON2", 5 + $v1); // succeeded
$v2 = array(10, 20);
trace("CON3", 5 + $v2[0]); // succeeded
trace("CON4", 5 + array(10, 20)[0]); // succeeded
trace("CON5", 5 + ++$v2[0]); // succeeded
trace("CON6", 5 + $v2[0]--); // succeeded
trace("CON7", 5 + (float)$v1); // succeeded
trace("CON8", 1 << $v1); // succeeded
trace("CON9", $v2[0] == $v2[1]); // succeeded
trace("CON10", $v2[0] = 123); // succeeded
trace("CON11", getValue() - 100); // succeeded
// involving constants
trace("CON21", 1 + CON1); // succeeded;
trace("CON22", 2 * CON2 + CON1); // succeeded;
//*/
///*
// try to define some constants with names not permitted as tokens
trace("36ABC", 100) . "\n"; // ill-formed name, but no error. Seems to work
trace("#%&", 200) . "\n"; // ill-formed name, but no error. Seems to work
//*/
///*
// try to redefine a user-defined constant
trace("MY_COLOR", "green"); // Warning, and doesn't change the value
// try to redefine a pre-defined constant
trace("TRUE", 999) . "\n"; // PHP5: No warning, and doesn't change the value, but
// constant("TRUE") and get_defined_constants show 999!
// HHVM: Constant TRUE already defined ...
echo " TRUE's value:" . TRUE . "\n"; // however, this shows the old value, 1
//*/
///*
// try to define some constants with non-scalar initial values
trace("COLORS", [10, 20]); // Works from PHP 7 onwards
class C {}
trace("MY_OBJECT", new C); // Constants may only evaluate to scalar values or arrays
$infile = fopen("Testfile.txt", 'r');
if ($infile == FALSE)
{
echo "Can't open file\n";
}
trace("MY_RESOURCE", $infile); // PHP5: Succeeded
// HHVM: Constants may only evaluate to scalar values
trace("MY_RESOURCE", $infile); // PHP5: Duplicate rejected
//*/
///*
class MyClass
{
// define("DEFINE_INSIDE_CLASS", 10); // not permitted inside a class; OK
// const CON30; // not permitted; OK
// const CON31 = 5 + 10.1 * 3;// failed; unexpected '+', expecting ',' or ';'
// const CON32 = $v1; // failed; unexpected '$v1'
// const CON33 = $v2[0]; // failed
// const CON34 = array(10, 20)[0]; // failed; Arrays are not allowed in class constants
// const CON35 = ++$v2[0]; // failed; unexpected '++'
// const CON37 = (float)10;// failed; unexpected '(float)' const CON38 = 1 << $v1); // surprise? succeeded
// const CON40 = new C; // failed; unexpected 'new'
const CON38 = 99; // succeeded
const CON39 = CON38; // succeeded
}
//*/
///*
// Note: As opposed to defining constants using define(), constants defined using the
// const keyword must be declared at the top-level scope because they are defined at
// compile-time. This means that they cannot be declared inside functions, loops or
// if statements.
// const CON50; // not permitted; OK
// const CON51 = 5 + 10.1 * 3;// failed; unexpected '+', expecting ',' or ';'
// const CON52 = $v1; // failed; unexpected '$v1'
// const CON53 = $v2[0]; // failed
// const CON54 = array(10, 20)[0]; // failed; Arrays are not allowed in class constants
// const CON55 = ++$v2[0]; // failed; unexpected '++'
// const CON57 = (float)10; // failed; unexpected '(float)' const CON38 = 1 << $v1); // surprise? succeeded
// const CON58 = new C; // failed; unexpected 'new'
const CON59 = 99; // succeeded
const CON60 = CON59; // succeeded
trace("CON61", 321);
const CON62 = CON61; // succeeded
trace("CON63", CON62);
function f($p)
{
// const CON70A = 10; // unexpected 'const'
trace("CON70B", 10); // succeeded
if ($p)
{
// const CON71A = 101; // unexpected 'const'
trace("CON71B", 101); // succeeded
}
}
f(10);
//*/
///*
// try defining a constant whose name is a keyword
trace("FOR", 100); // succeeded
// echo FOR; // unexpected 'FOR' (T_FOR)
// const FOR = 100; // unexpected 'FOR' (T_FOR)
//*/
class C3
{
const CON1 = 123; // implicitly static and can't say so explicitly
// implicitly public
public const CON2 = 123; // explicitly public
protected const CON3 = 123; // protected constant
private const CON4 = 123; // private constant
}
echo "CON1: " . C3::CON1 . "\n"; // use :: notation, as a const is implicitly static
// public const CON80 = 80; // top level const can not have visibility modifier
// protected const CON81 = 81; // top level const can not have visibility modifier
// private const CON82 = 82; // top level const can not have visibility modifier
//print_r(get_defined_constants());
--EXPECTF--
define STATUS1 succeeded; value is >1<
define MIN succeeded; value is >10<
define MAX succeeded; value is >20<
define MY_PI succeeded; value is >3.1415926<
define MY_COLOR succeeded; value is >red<
define C1 succeeded; value is ><
define CON1 succeeded; value is >35.3<
define CON2 succeeded; value is >15<
define CON3 succeeded; value is >15<
define CON4 succeeded; value is >15<
define CON5 succeeded; value is >16<
define CON6 succeeded; value is >16<
define CON7 succeeded; value is >15<
define CON8 succeeded; value is >1024<
define CON9 succeeded; value is ><
define CON10 succeeded; value is >123<
define CON11 succeeded; value is >150<
define CON21 succeeded; value is >36.3<
define CON22 succeeded; value is >65.3<
define 36ABC succeeded; value is >100<
define #%& succeeded; value is >200<
Notice: Constant MY_COLOR already defined in %s on line %d
define MY_COLOR failed; value is >red<
Notice: Constant TRUE already defined in %s on line %d
define TRUE failed; value is >1<
TRUE's value:1
define COLORS succeeded
Notice: Array to string conversion in %s on line %d
; value is >Array<
Warning: Constants may only evaluate to scalar values, arrays or resources in %s on line %d
define MY_OBJECT failed; not defined
Warning: fopen(Testfile.txt): failed to open stream: No such file or directory in %s on line %d
Can't open file
define MY_RESOURCE succeeded; value is ><
Notice: Constant MY_RESOURCE already defined in %s on line %d
define MY_RESOURCE failed; value is ><
define CON61 succeeded; value is >321<
define CON63 succeeded; value is >321<
define CON70B succeeded; value is >10<
define CON71B succeeded; value is >101<
define FOR succeeded; value is >100<
CON1: 123