A week back on November 28, 2019, and PHP is back with yet another update. The all-new version, 7.4, is introduced in the PHP 7 family and the predecessor or PHP 8.
For years, PHP has remained to be one of the most popular and widely used languages for web application development. According to W3Tech, nearly 78.9% of the websites are based on PHP. Having said that, it would not be wrong to state that PHP has a long way to go, even though the technology is age-old and doesn’t fall under the norms of modern-day fancied technology.
With time, the founders & contributors of the language have spent enormous time & effort, to get the technology tuned with the present-day developer expectations, and make it as optimal as possible. Keeping up to the same, last week, we witnessed a new release of PHP. Makers believe it to be one of the best versions to date.
Plenty of features, additional tools, support for typed programming & extremely fast execution speed of PHP 7.4 are your next web app development, partners.
One question that surfaces now and then is why PHP?
Whether you call it the ease of development or the simplicity of the language syntax, PHP is by far one of the simplest languages when it comes to web application development. Since the time of its inception, back in 2004, PHP has only grown. And this growth has been diverse, from the community to complexity, performance, and speed, the growing scale of PHP has been phenomenal. According to StackOverflow Developer Survey 2019, PHP ranks 8th in the list of top 10 programming languages. No wonder why leaders keep investing in technology.
Are you wondering what’s next? As we know the PHP has rolled out its new version, so let’s delve deeper into the concepts and find out what’s different in this version. So, ready?
Top Features to Watch Out In PHP 7.4
- Support For Arrow Functions
Remember the last time you had to write an array function in PHP. Lines of code and still, you could not get the perfect output. The fact that anonymous functions are a JavaScript feature, these appear to be verbose in PHP. With the new release, PHP renders support to the arrow functions and facilitate short closures when you just need to create a single line function. Needless to state that such a feature would reduce the efforts, time, and the total line of code, making it clean.
Not sure how? Let’s have it with an example to find the square of a number
Before 7.4
function square($n){ return ($n * $n); } $a = [1, 2, 3, 4, 5]; $b = array_map('square', $a); print_r($b);
After 7.4
$a = [1, 2, 3, 4, 5]; $b = array_map(fn($n) => $n * $n, $a); print_r($b)
Seems better and easy, right? Well, that’s the new version of PHP for you.
Earlier, the anonymous variables put the “use” language construct in action where variables were inherited from the parent. However, with PHP 7.4, the parent scope variables are captured implicitly by value, promoting a one-liner function.
It’s been long since you have had used the concept of typed property in PHP? No!! PHP hardly had the terminology of type property. But don’t worry. The new release of PHP maps the need. In case you are new to the field and not aware of what type of property actually means? Let me tell you that typed property gives a developer the ease to declare hints both to the property and the variables. Earlier to embed type contracts, developers had to make use of the getter and setter methods.
Additionally, PHP developers can effectively declare the type of static properties in lieu of the declaration methods adopted for class properties as well as class variables. To have a better idea, let’s consider the following example:
Class User { /** @var int $id */ private $id; /** @var string $name */ private $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } public function getId(): int { return $this->id; } public function setId(int $id): void { $this->id = $id; } public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } }
Well, that was the piece of code you had to write before the new release. Now, with PHP 7.4, the above would change to
class User { public int $id; public string $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } }
And the definition for the static types be:
public static iterable $staticProp;
The allowed types that you can use apart from callable and void include:
Int
Float
String
Iterable
Array
Object
Self
Parent
Bool
For instances where a different value is assigned to the type, a comprehensive message is thrown by PHP.
Here again, let’s refer to an example where $name is declared as a string, and a numerical value is assigned to the same. Find the error displayed below.
Class User { public int $id; public string $name; } $user = new User; $user->id = 42; $user->name = 32; // Uncaught TypeError: Typed property User::$name // must be string, number used
Hope this clears your doubt relating to the typed properties.
PHP 7.4 welcomes a new operator, one that we call as Coalesce Assign (??=) Operator. It is proposed that in spite of ?? coalescing operator as used to be the comparison operator, the new version coalesce equal or ??= operator acts as the assignment operator. For instances where the left parameter is kept null, the value of the parameter right to it is assigned to the same. And if the value is anything but null, no changes are made.
Having doubts or not sure how to use the coalesce operator in PHP 7.4. Let’s get to it through an example:
$someArray['key'] ??= 'someValue'; instead of $someArray['key'] = $someArray['key'] ?? 'someValue';
So, here in case, the left-hand parameter’s value is null, there occurs an automated copying step to assign the value of the right-hand parameter to the left.
Performance happens to be one of the most essential aspects when it comes to developing a web application. No doubt, PHP has exceptionally high performance, but that doesn’t mean it cannot be improved further. The concept of preloading ensures that all of the frameworks and files are effectively loaded in OPcache. Every time you work on a framework, all of the files need to be downloaded and fetched.
So, when you first configure OPcache, the code specific files engage in the request processing, which is then checked the time and again for updates. Evidently, preloading maps the field in the shared memory, and this makes them available 24/7.
With this, developers can easily link the object that promotes destruction and also saves them. This enables developers to embed structures that are cache-like. Further, such references can never be serialized.
Example:
< ?php $obj = new stdClass; $weakref = WeakReference::create($obj); var_dump($weakref->get()); unset($obj); var_dump($weakref->get()); ? >
Conclusion
This is just the tip of the iceberg, and there is more to the technology than you can anticipate. Whether it is the performance or the speed, the memory usage or the cleaner code, the all-new version of PHP encompasses all.