Hey PHP fans! This article highlights some excellent new features of our favorite scripting language. Whether you're a seasoned pro or...
Hey PHP fans! This article highlights some excellent new features of our favorite scripting language. Whether you're a seasoned pro or just starting out, these will make your coding life easier and more fun. Let's dive into the top PHP features you can use right now!
Let's say you don't want changes to a variable after initialization. Now, with readonly properties, you can set a property once and prevent it from being modified.
class User {
public readonly string $username;
public function __construct(string $username) {
$this->username = $username;
}
}
Enums are now a thing in PHP! They let you define a set of named values, perfect for things like statuses or categories.
enum Status {
case PENDING;
case ACTIVE;
case INACTIVE;
}
$status = Status::ACTIVE;
Match expression is a more flexible alternative to switch statements. They let you return values directly from each case.
$status = 'active';
$message = match ($status) {
'active' => 'The user is active.',
'inactive' => 'The user is inactive.',
'pending' => 'The user is pending.',
default => 'Unknown status.',
};
Save time by defining and initializing properties directly in the constructor.
class Point {
public function __construct(
public float $x,
public float $y
) {}
}
$point = new Point(1.5, 2.5);
Named arguments make your code more readable by allowing you to pass values to a function by name instead of position.
function createUser(string $username, bool $isAdmin = false) {
// Your code here
}
createUser(username: 'john_doe', isAdmin: true);
Avoid those annoying null checks with the nullsafe operator, which lets you call methods or access properties on an object only if it's not null.
$user = getUser();
$profile = $user?->getProfile()?->getBio();
Type hinting just got more flexible with union types, allowing you to specify multiple types for a parameter or return value.
function processNumber(int|float $number): int|float {
return $number * 2;
}
Array unpacking with string keys, making it easier to merge arrays.
$array1 = ['a' => 1, 'b' => 2];
$array2 = ['c' => 3, ...$array1];
print_r($array2);
// Output: ['c' => 3, 'a' => 1, 'b' => 2]
With PHP 8.3, you can enable json.exceptions to throw JsonException by default on JSON errors.
ini_set('json.exceptions', '1');
try {
$data = json_decode('{"invalidJson":}', true);
} catch (JsonException $e) {
echo 'JSON Error: ' . $e->getMessage();
}
Just-In-Time (JIT) compilation is now part of PHP, making your scripts run faster by compiling parts of the code at runtime.
2024 is shaping up to be an exciting year for PHP developers. With these new features, you'll write cleaner, faster, and more readable code. So update your PHP version and start playing with these cool new features.
All the above have been previously shared on our Twitter, one by one. Follow us on Twitter; You'll ❤️ it. You can also check our FREE Laravel Advanced series to know trending Laravel features. Keep exploring, and keep coding. Until next time, happy coding! 🚀
Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.
What do you think about this?
Wondering what our community has been up to?