@@ -128,6 +128,47 @@ Any new types and keywords added to future PHP versions MUST be in lower case.
128
128
Short form of type keywords MUST be used i.e. ` bool ` instead of ` boolean ` ,
129
129
` int ` instead of ` integer ` etc.
130
130
131
+ ### 2.6 Trailing commas
132
+
133
+ Numerous PHP constructs allow a sequence of values to be separated by a comma,
134
+ and the final item may have an optional comma. Examples include array key/value pairs,
135
+ function arguments, closure ` use ` statements, ` match() ` statement branches, etc.
136
+
137
+ If that list is contained on a single line, then the last item MUST NOT have a trailing comma.
138
+
139
+ If the list is split across multiple lines, then the last item MUST have a trailing comma.
140
+
141
+ The following are examples of correct comma placement:
142
+
143
+ ``` php
144
+ function beep(string $a, string $b, string $c)
145
+ {
146
+ // ...
147
+ }
148
+
149
+ function beep(
150
+ string $a,
151
+ string $b,
152
+ string $c,
153
+ ) {
154
+ // ...
155
+ }
156
+
157
+ $arr = ['a' => 'A', 'b' => 'B', 'c' => 'C'];
158
+
159
+ $arr = [
160
+ 'a' => 'A',
161
+ 'b' => 'B',
162
+ 'c' => 'C',
163
+ ];
164
+
165
+ $result = match ($a) {
166
+ 'foo' => 'Foo',
167
+ 'bar' => 'Bar',
168
+ default => 'Baz',
169
+ };
170
+ ```
171
+
131
172
## 3. Declare Statements, Namespace, and Import Statements
132
173
133
174
The header of a PHP file may consist of a number of different blocks. If present,
@@ -231,7 +272,7 @@ For example:
231
272
</html >
232
273
```
233
274
234
- Declare statements MUST contain no spaces and MUST be exactly ` declare(strict_types=1) `
275
+ Declare statements MUST NOT contain any spaces and MUST be exactly ` declare(strict_types=1) `
235
276
(with an optional semicolon terminator).
236
277
237
278
Block declare statements are allowed and MUST be formatted as below. Note position of
@@ -590,13 +631,19 @@ public function process(string $algorithm, &...$parts)
590
631
}
591
632
```
592
633
593
- ### 4.6 ` abstract ` , ` final ` , and ` static `
634
+ ### 4.6 Modifier Keywords
594
635
595
- When present, the ` abstract ` and ` final ` declarations MUST precede the
596
- visibility declaration.
636
+ Properties and methods of a class have numerous keyword modifiers that alter how the
637
+ engine and language handles them. When present, they MUST be in the following order:
597
638
598
- When present, the ` static ` declaration MUST come after the visibility
599
- declaration.
639
+ * Inheritance modifier: ` abstract ` or ` final `
640
+ * Visibility modifier: ` public ` , ` protected ` , or ` private `
641
+ * Scope modifier: ` static `
642
+ * Mutation modifier: ` readonly `
643
+ * Type declaration
644
+ * Name
645
+
646
+ All keywords MUST be on a single line, and MUST be separated by a single space.
600
647
601
648
``` php
602
649
<?php
@@ -605,7 +652,9 @@ namespace Vendor\Package;
605
652
606
653
abstract class ClassName
607
654
{
608
- protected static $foo;
655
+ protected static readonly string $foo;
656
+
657
+ final protected int $beep;
609
658
610
659
abstract protected function zim();
611
660
@@ -614,6 +663,11 @@ abstract class ClassName
614
663
// method body
615
664
}
616
665
}
666
+
667
+ readonly class ValueObject
668
+ {
669
+ // ...
670
+ }
617
671
```
618
672
619
673
### 4.7 Method and Function Calls
@@ -1058,6 +1112,36 @@ $foo->bar(
1058
1112
);
1059
1113
```
1060
1114
1115
+ ### 7.1 Short Closures
1116
+
1117
+ Short closures, also known as arrow functions, MUST follow the same guidelines
1118
+ and principles as long closures above, with the following additions.
1119
+
1120
+ The ` fn ` keyword MUST be preceded and succeeded by a space.
1121
+
1122
+ The ` => ` symbol MUST be preceded and succeeded by a space.
1123
+
1124
+ The semicolon at the end of the expression MUST NOT be preceded by a space.
1125
+
1126
+ The expression portion MAY be split to a subsequent line. If so, the ` => ` MUST be included
1127
+ on the second line, and MUST be indented once.
1128
+
1129
+ The following examples show proper common usage of short closures.
1130
+
1131
+ ``` php
1132
+
1133
+ $func = fn (int $x, int $y): int => $x + $y;
1134
+
1135
+ $func = fn (int $x, int $y): int
1136
+ => $x + $y;
1137
+
1138
+ $func = fn (
1139
+ int $x,
1140
+ int $y
1141
+ ): int
1142
+ => $x + $y;
1143
+ ```
1144
+
1061
1145
## 8. Anonymous Classes
1062
1146
1063
1147
Anonymous Classes MUST follow the same guidelines and principles as closures
@@ -1118,6 +1202,70 @@ enum Suit: string
1118
1202
const Wild = self::Spades;
1119
1203
}
1120
1204
1205
+ ## 10. Heredoc and Nowdoc
1206
+
1207
+ A nowdoc SHOULD be used wherever possible. Heredoc MAY be used when a nowdoc
1208
+ does not satisfy requirements.
1209
+
1210
+ Heredoc and nowdoc syntax is largely governed by PHP requirements with the only
1211
+ allowed variation being indentation. Declared heredocs or nowdocs MUST
1212
+ begin on the same line as the context the declaration is being used in.
1213
+ Subsequent lines in the heredoc or nowdoc MUST be indented once past the scope
1214
+ indentation they are declared in.
1215
+
1216
+ The following is ***not allowed*** due to the heredoc beginning on a
1217
+ different line than the context it's being declared in:
1218
+ ```php
1219
+ $notAllowed =
1220
+ <<<'COUNTEREXAMPLE'
1221
+ This
1222
+ is
1223
+ not
1224
+ allowed.
1225
+ COUNTEREXAMPLE;
1226
+ ```
1227
+
1228
+ Instead the heredoc MUST be declared on the same line as the variable
1229
+ declaration it's being set against.
1230
+
1231
+ The follow is *** not allowed*** due to the scope indention not matching the scope the
1232
+ heredoc is declared in:
1233
+ ``` php
1234
+ function notAllowed()
1235
+ {
1236
+ $notAllowed = <<<'COUNTEREXAMPLE'
1237
+ This
1238
+ is
1239
+ not
1240
+ allowed.
1241
+ COUNTEREXAMPLE
1242
+ }
1243
+ ```
1244
+
1245
+ Instead, the heredoc MUST be indented once past the indentation of the scope
1246
+ it's declared in.
1247
+
1248
+ The following is an example of both a heredoc and a nowdoc declared in a
1249
+ compliant way:
1250
+ ``` php
1251
+ function allowed()
1252
+ {
1253
+ $allowed = <<<COMPLIANT
1254
+ This
1255
+ is
1256
+ a
1257
+ compliant
1258
+ heredoc
1259
+ COMPLIANT;
1260
+
1261
+ $allowedNowdoc = <<<' COMPLIANT'
1262
+ This
1263
+ is
1264
+ a
1265
+ compliant
1266
+ heredoc
1267
+ COMPLIANT;
1268
+ }
1121
1269
```
1122
1270
1123
1271
[PSR-1]: https: //www.php-fig.org /psr /psr-1 /
0 commit comments