Kritim Yantra
Apr 25, 2025
PHP has come a long way over the years. From a simple scripting language to a powerful backend framework, it's now packed with modern syntax that makes your code cleaner, faster, and easier to understand.
And if you’re using Laravel, the good news is—it fully supports all these modern PHP features!
In this guide, we'll break down how to use modern PHP syntax in your Laravel projects, even if you're new to programming.
Starting with PHP 7.4 and especially in PHP 8+, developers have access to powerful new features like:
These features help you:
Tip: Upgrade to PHP 8.2+ for the best experience with Laravel.
Let your code speak for itself by telling PHP what data types your functions accept and return.
public function getUser($id) {
return User::find($id);
}
public function getUser(int $id): ?User {
return User::find($id);
}
💡 int $id
ensures type safety. ?User
means it returns a User
or null
.
public function store(Request $request): RedirectResponse {
$validated = $request->validate(['title' => 'required|string']);
Post::create($validated);
return redirect()->route('posts.index');
}
Arrow functions are shorthand for anonymous functions—perfect for collections and callbacks.
$squared = array_map(function ($n) {
return $n * $n;
}, [1, 2, 3]);
$squared = array_map(fn($n) => $n * $n, [1, 2, 3]);
$emails = User::all()->map(fn(User $user) => $user->email);
??
and ??=
)Simplify fallback values with the null coalescing operator.
$role = isset($_GET['role']) ? $_GET['role'] : 'guest';
$role = $_GET['role'] ?? 'guest';
$name = $request->input('name') ?? 'Anonymous';
Improve readability and flexibility by specifying argument names.
redirect('/', 302, ['X-Header' => 'Value']);
redirect(to: '/', headers: ['X-Header' => 'Value'], status: 302);
Enums define a set of constant values, perfect for things like status codes.
enum PostStatus: string {
case Draft = 'draft';
case Published = 'published';
}
class Post extends Model {
protected $casts = [
'status' => PostStatus::class,
];
}
$post->status = PostStatus::Published;
Replace switch
with the cleaner and more predictable match
.
switch
:switch ($code) {
case 200: $msg = 'OK'; break;
case 404: $msg = 'Not Found'; break;
default: $msg = 'Unknown';
}
match
:$msg = match ($code) {
200 => 'OK',
404 => 'Not Found',
default => 'Unknown',
};
$errorMessage = match ($exception->getCode()) {
403 => 'Forbidden',
404 => 'Page Not Found',
default => 'Something went wrong',
};
Cut down on boilerplate when injecting dependencies.
class PaymentService {
protected PaymentGateway $gateway;
public function __construct(PaymentGateway $gateway) {
$this->gateway = $gateway;
}
}
class PaymentService {
public function __construct(protected PaymentGateway $gateway) {}
}
class UserController extends Controller {
public function __construct(protected UserRepository $users) {}
}
Merge arrays cleanly using the spread operator.
$array1 = [1, 2];
$array2 = [3, 4];
$merged = [...$array1, ...$array2]; // [1, 2, 3, 4]
$default = ['mode' => 'dark', 'lang' => 'en'];
$userSettings = ['lang' => 'fr'];
$final = [...$default, ...$userSettings]; // ['mode' => 'dark', 'lang' => 'fr']
🔧 Update PHP to at least version 8.1+
🧱 Use Laravel 9 or 10 for full compatibility
🪄 Refactor gradually—no need to rewrite everything all at once
📚 Keep learning new features as PHP evolves!
Modern PHP syntax isn’t just about writing fancy code—it’s about making your Laravel applications cleaner, faster, and easier to maintain.
By using features like:
...you’ll feel the power of writing professional-grade PHP code.
Start small. Upgrade gradually. Keep leveling up your skills. You’ve got this! 💪
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google