If youβre still writing PHP like it's 2015, you're missing out on some powerful features that make your code cleaner, safer, and easier to maintain.
In this post, Iβll walk you through 5 modern PHP features in a very simple way β with examples you can actually use in real projects.
1. β Typed Properties (Strict Data Handling)
Before PHP 7.4, you couldnβt define types for class properties.
β Old way:
class User {
public $name;
}
β New way:
class User {
public string $name;
}
Why it matters:
- Prevents invalid data
- Makes your code predictable
- Helps avoid bugs early
2. β Arrow Functions (Shorter Closures)
Arrow functions make your code shorter and cleaner.
β Old way:
$numbers = [1, 2, 3];
$squared = array_map(function($n) {
return $n * $n;
}, $numbers);
β New way:
$squared = array_map(fn($n) => $n * $n, [1,2,3]);
Why it matters:
- Less boilerplate
- More readable functional code
3. β
Null Safe Operator (?->)
Avoid those annoying null checks.
β Old way:
if ($user && $user->profile) {
echo $user->profile->email;
}
β New way:
echo $user?->profile?->email;
Why it matters:
- Cleaner code
- No more "trying to access property of null" errors
4. β Match Expression (Better than switch)
match is more strict and cleaner than switch.
β switch:
switch($status) {
case 'success':
$msg = 'Done';
break;
case 'error':
$msg = 'Failed';
break;
}
β match:
$msg = match($status) {
'success' => 'Done',
'error' => 'Failed',
default => 'Unknown'
};
Why it matters:
- No
breakneeded - Strict comparison (no type confusion)
5. β Constructor Property Promotion
Less boilerplate when creating classes.
β Old way:
class User {
public string $name;
public function __construct($name) {
$this->name = $name;
}
}
β New way:
class User {
public function __construct(public string $name) {}
}
Why it matters:
- Cleaner classes
- Less repetitive code
π₯ Final Thoughts
Modern PHP is:
- Faster β‘
- Cleaner β¨
- Safer π
If you're working with frameworks like Laravel, these features are already being used under the hood β so understanding them will level up your coding skills.
π¬ What Next?
If you found this useful, I can share:
- Real-world Laravel examples using these features
- Performance tips for PHP apps
- Clean architecture patterns in PHP
Let me know in the comments π
Top comments (0)