What Is New in PHP 7.4
| Category | Key Changes |
|---|---|
| New Features | Typed Properties, Arrow Functions, Spread Operator in Arrays, Preloading |
| Improvements | FFI Extension, Weak References, Custom Object Serialization |
| Deprecations | Left-associative Ternary Operator, Nested Ternary without Parentheses, Curly Braces for Array/String Access |
| Other Changes | Password Hashing Registry, Numeric Literal Separator, mb_str_split function |
What are the major new language features in PHP 7.4?
PHP 7.4 introduces several syntax enhancements that make code more expressive and type-safe. Typed properties allow class properties to declare their type, which catches type errors early. Arrow functions provide a cleaner syntax for short closures, reducing boilerplate code.
The spread operator (...) now works for array literals, making array merging more intuitive. This is similar to how it works in argument unpacking, but for arrays.
Example: Typed Properties
class User {
public int $id;
public string $name;
public ?Profile $profile; // Nullable type
}
How does preloading improve performance?
Preloading is a significant performance feature for OPcache. It allows the server to load a set of PHP files into memory at startup, making them permanently available for all subsequent requests.
This eliminates the need to compile these scripts on every request, which reduces overhead and improves response times. In practice, this benefits frameworks that use a large codebase, as their core classes are always ready to execute.
What deprecations should developers watch out for?
Several old behaviors are now deprecated and will generate warnings. Using curly braces for array and string offset access (e.g., $array{0}) is deprecated in favor of square brackets.
The nested ternary operator without explicit parentheses and the left-associative ternary operator are also deprecated due to confusion in evaluation order. These changes encourage clearer, less ambiguous code.
Deprecated Syntax
$a = $b[0]; // Correct
$a = $b{0}; // Deprecated
$c = $a ? $b : $c ? $d : $e; // Deprecated (ambiguous)
$c = ($a ? $b : $c) ? $d : $e; // Explicit
What are the new core extensions and improvements?
The Foreign Function Interface (FFI) extension is now available, allowing PHP to call functions from shared libraries and interact with C data structures. This is powerful for high-performance tasks or integrating with low-level libraries.
Weak References allow the programmer to retain a reference to an object that does not prevent the object from being destroyed by the garbage collector. This is useful for implementing caches.
FAQ
Is the spread operator for arrays the same as argument unpacking?
Yes, the syntax is identical (...), but it is now used within array literals to merge arrays, e.g., $newArray = [...$array1, ...$array2];.
Can I use typed properties with any type?
Yes, all supported types work: bool, int, float, string, array, object, iterable, self, parent, and nullable types with ?.
What happens if I assign a wrong type to a typed property?
A TypeError is thrown immediately, which helps catch bugs much earlier in development compared to untyped code.
Is the FFI extension enabled by default?
No, the FFI extension is not enabled by default. It must be explicitly enabled via the ffi.enable directive in your php.ini configuration file.
Are there any new functions added?
Yes, mb_str_split() is a new function that splits a multibyte string into an array, similar to str_split() but for multibyte encodings.