Php 8.4: features, improvements, and deprecations you need to know
PHP 8.4 introduces several exciting features and improvements while deprecating or removing outdated functionality. Here’s a comprehensive overview of what’s new and what’s changed.
🚀 Key Features and Improvements in PHP 8.4
1. Property Hooks
Property hooks allow custom behavior when accessing, modifying, or unsetting class properties. This eliminates the need for boilerplate getter and setter methods.
Example:
class Demo { private string $name; // Getter for 'name' with a hook to modify its value public function __get(string $name): mixed { if ($name === 'name') { return strtoupper($this->$name); // Modify property on get } return null; } // Setter for 'name' with a hook to modify its value public function __set(string $name, mixed $value): void { if ($name === 'name') { $this->$name = strtolower($value); // Modify value before setting } } }
$demo = new Demo(); $demo->name = 'John Doe'; // Automatically converted to lowercase echo $demo->name; // Outputs: JOHN DOE
2. Asymmetric Visibility
You can now define different visibility levels for property getters and setters. For instance, a property can have a public
getter but a private
setter.
Example:
class User { private string $password; // Public getter for the password public function getPassword(): string { return $this->password; } // Private setter for the password private function setPassword(string $password): void { $this->password = $password; } public function __construct(string $password) { $this->setPassword($password); // Set the password using the private setter } }
$user = new User('supersecret'); echo $user->getPassword(); // Outputs: supersecret
MyClass()->method()
Without Parentheses
3. PHP 8.4 simplifies syntax by allowing methods without parentheses if they take no parameters.
Example:
class Example { public function hello() { return "Hello, World!"; } }
// No parentheses needed echo new Example()->hello(); // Outputs: Hello, World!
4. HTML5 Support
PHP 8.4 improves compatibility with modern web development by enhancing functions like htmlspecialchars()
and htmlentities()
for better handling of HTML5 attributes and entities.
Example:
$htmlContent = '<input type="text" value="John & Jane">';
echo htmlspecialchars($htmlContent, ENT_QUOTES | ENT_HTML5);
// Outputs: <input type="text" value="John & Jane">
5. JIT Changes
The Just-In-Time (JIT) compiler has been refined for better performance in real-world scenarios, particularly for computation-heavy scripts and long-running processes. You can control JIT via opcache.jit
.
6. Lazy Objects
Lazy objects are instantiated only when accessed, improving memory usage and performance in applications with complex objects.
Example:
// Lazy initialization of a complex object
$lazyObject = new LazyObject(fn() => new ExpensiveClass());
This helps in cases where creating the object upfront would be too costly, and its initialization is deferred until needed.
7. Exit and Die as Functions
The exit
and die
constructs are now proper functions, enabling their use in variable function calls or callbacks.
Example:
// Now supports variable function calls
$func = 'exit';
$func('Goodbye!'); // Outputs: Goodbye!
8. Multibyte Trim Functions
With mb_trim()
, PHP 8.4 introduces trimming functions that handle multibyte characters, making string manipulation more reliable for non-ASCII languages.
Example:
$text = " こんにちは ";
echo mb_trim($text); // Outputs: こんにちは
This feature is crucial for working with languages like Japanese, Chinese, and Korean, where regular trimming functions may not work correctly.
📉 Deprecations in PHP 8.4
PHP 8.4 deprecates several outdated features, encouraging developers to adopt modern practices. Below is the complete list of deprecations and removals.
1. DOMDocument and DOMEntity Properties
The soft-deprecated properties in DOMDocument
and DOMEntity
are formally deprecated.
2. E_STRICT Constant
The E_STRICT
constant has been deprecated since its functionality is already included in other error levels.
strtok()
3. The strtok()
function is deprecated due to its limited usage compared to modern alternatives like explode()
or preg_split()
.
4. User Output Handlers
Two key deprecations include:
- Returning non-string values from user-defined output handlers.
- Producing output directly within an output handler.
file_put_contents()
with Arrays
5. Passing an array as $data
to file_put_contents()
is no longer supported. Use a string instead.
Example:
// Deprecated: file_put_contents('file.txt', ['Hello', 'World']);
// Correct approach: file_put_contents('file.txt', "Hello\nWorld");
6. MySQLi Deprecations
Several MySQLi functions are deprecated, including:
mysqli_ping()
mysqli_refresh()
mysqli_kill()
- Second parameter for
mysqli_store_result()
7. DOMImplementation::getFeature()
The DOMImplementation::getFeature()
method has been removed.
8. Deprecation of Hashing Functions
The following functions are deprecated due to security and performance concerns:
md5()
sha1()
md5_file()
sha1_file()
uniqid()
and lcg_value()
9. These functions are deprecated for their inability to generate cryptographically secure or reliable unique values.
10. Other Notable Deprecations
- Session configurations:
session.sid_length
andsession.sid_bits_per_character
. - Legacy constants:
SUNFUNCS_RET_STRING
,SUNFUNCS_RET_DOUBLE
,SUNFUNCS_RET_TIMESTAMP
. - SOAP_FUNCTIONS_ALL constant for
SoapServer::addFunction()
. - Deprecation of proprietary CSV escaping mechanisms.
Conclusion
PHP 8.4 builds upon its strong foundation with powerful features like property hooks, lazy objects, and asymmetric visibility, while addressing outdated and insecure functionalities. It strikes a balance between innovation and modernization, making it a must-consider upgrade for developers.
Are you ready to adopt PHP 8.4? Let us know your thoughts!