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!
1. Readonly Properties
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;
}
}
2. Enums
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;
3. Match Expressions
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.',
};
4. Constructor Property Promotion
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);
5. Named Arguments
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);
6. Nullsafe Operator
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();
7. Union Types
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;
}
8. Array Unpacking with String Keys
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]
9. JSON_THROW_ON_ERROR
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();
}
10. JIT Compilation
Just-In-Time (JIT) compilation is now part of PHP, making your scripts run faster by compiling parts of the code at runtime.
Wrapping Up
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! 🚀
Top comments (1)
Nice