We are excited to announce that PHP 8.4 (Beta 3) is now available for testing on all SiteGround servers – well ahead of its official release date of November 21, 2024. Once again, we are among the first companies to offer PHP 8.4 (Beta 3) for testing on our hosting platform. Thanks to our unique PHP server setup, we can offer the latest PHP versions for our customers to safely test on their websites hosted with us.
Find out more about the new features brought by this latest version of PHP in the next section.
What's new in PHP 8.4 (Beta 3)
This latest release introduces some very important new features that will further improve the PHP development experience. Let's dive into some of the biggest changes that PHP 8.4 (Beta 3) offers:
Property Hooks
One of the new features in PHP 8.4 (Beta 3) includes the ability to define property hooks which will eliminate the need for a lot of boilerplate code. This is one of the biggest changes in the history of PHP. Property hooks will help eliminate a lot of getters and setters by allowing each property to define its own getter and set hooks. What's more, an exciting addition is that in PHP 8.4 (Beta 3) property hooks can now be defined on interfaces.
We can take as an example this fairly standard class:
<?php
declare(strict_types=1);
class Website
{
private $domain;
public function __construct(string $domain) {
$this->domain = $domain;
}
public function getDomain(): string {
return $this->domain;
}
public function setDomain(string $domain): void {
if (strlen($domain) === 0) {
throw new ValueError("Domain must be non-empty");
}
$this->domain = $domain;
}
}
With PHP 8.4 (Beta 3) we can use property hooks to achieve the same result with the following syntax:
<?php
declare(strict_types=1);
class Website
{
public string $domain {
set {
if (strlen($value) === 0) {
throw new ValueError("Domain must be non-empty");
}
$this->domain = $value;
}
get => $this->domain;
}
public function __construct(string $domain) {
$this->domain = $domain;
}
}
new without additional parentheses
Another new feature that will save you a lot of code is that you no longer have to wrap newly created objects in parentheses in order to chain methods on them.
Plus, it doesn't just work with methods, you can also chain properties, static methods, constants… basically anything you want. In short, the new feature will simplify syntax and make code more concise and readable.
Example:
The “access class members on instantiation” feature was introduced in PHP 5.4.0. Since then, constants, properties, and methods can be accessed on a newly created instance without an intermediate variable, but only if the new expression is wrapped in parentheses:
class Website
{
...
public function getDomain(): string {
return $this->domain;
}
}
// Valid syntax
$myDomain = (new Website('siteground.com'))->getDomain();
// Invalid syntax until PHP8.4
$myDomain = new Website('siteground.com')->getDomain();
New Array Functions
Another notable change in PHP is the introduction of several new Array functions. PHP 8.4 introduced several new array functions with callback functionality:
array_find()
Function that returns the value of the first element in the array that matches the condition. If none of the elements match the condition, the function will return `null`.
array_find_key()
Returns the key of the first element for which the callback was true. If none of the elements satisfy the condition, the function returns `null`. array_all() Checks whether the callback returns true for all elements in the array.
array_any()
Checks that the callback returns true for any of the elements in an cambodia whatsapp number data array.
These new features make it easier to search and manipulate arrays based on custom conditions, which will overall result in cleaner, easier-to-read code.
How to test PHP 8.4 on SiteGround servers
As a SiteGround customer, you can easily test PHP 8.4 (Beta 3) on your website. Simply go to your Site Tools > Developers > PHP Manager and you can replace the current PHP version used by your site with PHP 8.4 (Beta 3) in one click.
Please note that PHP 8.4 (Beta 3) is primarily available for testing purposes, and as usual, we strongly recommend that you do not use it on your production site before its official scheduled release date (November 21, 2024).
If you need to test PHP 8.4 (Beta 3) with your existing site, we recommend that you create a new staging site and clone your production site onto it to test different things. To do this, you can use our WordPress staging tool . While you're testing, make sure nothing is failing and check your log files for any warnings or errors. Once you're done testing, you can simply delete that site.
PHP 8.4 (Beta 3) Now Available for Testing on SiteGround Servers
-
- Posts: 14
- Joined: Wed Dec 18, 2024 3:05 am