PHP's coding style is just about to get its new version. 3.1 fixes a few clarity and wording issues but also there are some interesting changes. You can check these yourself, there's a changelog. I'll, instead provide a few numbered code snippets to discuss.
🔸1 — clone with parenthesis
$b = clone($a);
$b = clone($a, [
'foo' => 'bar',
]);
🔸2 — switch-case-match
switch (true) {
case (
$a === 10
&& $b === 20
):
doSomething();
break;
}🔸3 — spaces around pipe operator
$result = $input |> trim(...) |> strtoupper(...);
🔸4 — chaining
$result = '<foo>'
|> strtoupper(...)
|> htmlspecialchars(...);
🔸5 — empty closures
$noOpFunction = function () {};
// SHOULD be preferred where possible:
$noOpFunction = fn() => null;🔸6 — anonymous classes
$example = new
#[Attribute]
class {
// ...
};
🔸7 — enums and scoping (private)
<?php
enum Size
{
case Small;
case Medium;
case Large;
private const Huge = self::Large;
}
🔸8 - arrays
return [
'foo',
'bar',
];
someFunction([
'foo',
'bar',
]);
#php #psr12 #percs