PHP 8.4 release: Practical changes you can make to improve code quality today!
Discover PHP 8.4 features to boost your code quality now
PHP 8.4, released on November 21, 2024, is loaded with exciting new features that can elevate your coding experience. Trust me, thereβs plenty to be excited about! But instead of covering every single change, this article will spotlight some practical updates you can start using today to improve your code quality with the latest version.
In this article, we will cover four major updates:
Property hooks
Asymmetric visibility
new
without parenthesesarray_find
Prerequisites:
- A basic understanding of Object-Oriented Programming in PHP
Property hooks
Write less code and get the same result? I'm all for that! πͺπ½
A hook lets you automatically perform a specific action when something happens in our code. For instance, when setting a value for the firstName
property of an object, you might want to automatically change the first letter of the value to uppercase.
Typically, here is what your code might like without property hooks: ππ½
class Person
{
private string $firstName;
public function getFirstName(): string
{
return $this->firstName;
}
public function setFirstName(string $value): void
{
$this->firstName = ucfirst(strtolower($value));
}
}
$person = new Person();
$person->setFirstName('peter');
print $person->getFirstName(); -β>>> "Peter"
Property hooks in classes
Property hooks let you attach get
and set
hooks directly to a property, eliminating the need to create separate getter and setter methods as before. These hooks are optional, so you can define both if needed, or just one.
Here's what we have now using the get
and set
property hooks: ππ½
class Person
{
public string $firstName {
set => $this->firstName = ucfirst(strtolower($value));
get => $this->firstName;
}
}
$person = new Person();
$person->firstName = 'peter';
print $person->firstName; β->>> "Peter"
Property hooks in interfaces
I absolutely love β€οΈ that property hooks work with interfaces!
Assuming you have an interface that defines getter and setter methods for the firstName
property in all implementing classes, here is what your code will look like:ππ½
interface HasFirstName
{
public function getFirstName(): string;
public function setFirstName(string $name): void;
}
class Person implements HasFirstName
{
private string $firstName;
public function getFirstName(): string
{
return $this->firstName;
}
public function setFirstName(string $firstName): void
{
$this->firstName = ucfirst(strtolower($firstName));
}
}
$person = new Person();
$person->setFirstName('peter');
print $person->getFirstName(); β->>> "Peter"
When we update the syntax to use property hooks, we now have this: ππ½
interface HasFirstName
{
public string $firstName {get; set;}
}
class Person implements HasFirstName
{
public string $firstName{
set => $this->firstName = ucfirst(strtolower($value));
get => $this->firstName;
}
}
$person = new Person();
$person->firstName = 'peter';
print $person->firstName; β->>> "Peter"
This looks sweet!! and I canβt wait to see you use it in your code. Letβs jump right to the next improvement. ππ½
Asymmetric visibility
PHP currently has the ability to control the visibility of properties in a class using access modifiers like public
, private
, or protected
. Before the latest updates in PHP 8.4, these access modifiers are symmetric, which means the same modifier attached to a property of a class applies to the get and set operations for that property.
For example, if you add a property called uniqueId
to the Person
class and want it to be public for reading but private for writing, you would need to define getter and setter methods. Your code might look like this: ππ½
class Person
{
private string $uniqueId = "unique-for-johndoe";
public function getUniqueId(): string
{
return $this->uniqueId;
}
}
$person = new Person();
print $person->uniqueId; // Error. Cannot read private property
In our example, when we say uniqueId
is a private
property, it means it is private
for both reading and writing from outside the Person
class. The only way to read the uniqueId
property is by using the public getter method: $this->getUniqueId()
.
The idea behind Asymmetric visibility is to let properties have different visibility settings for reading and writing, using just one definition, without needing getter and setter methods. Now, let's update the Person
class to use Asymmetric visibility: ππ½
class Person
{
public private(set) string $uniqueId = "unique-for-johndoe";
}
$person = new Person();
print $person->uniqueId; β->>> "unique-for-johndoe"
Now, the uniqueId
has two access modifiers instead of one. The public
modifier allows reading access, while the private(set)
modifier controls writing access for the uniqueId
.
new
Without parentheses
This is another update I'm excited about. Before the new
without parentheses update, if I wanted to create an instance of the person class using the new
keyword and immediately access the uniqueId
on the instance without saving it in a variable, here's how I would do it: ππ½
print (new Person())->getUniqueId();
Notice anything?
It may not be easily noticeable, but the new Person()
is wrapped in extra parentheses ()
before calling the getUniqueId()
method. Without the extra parentheses, PHP would throw a syntax error.
In PHP 8.4, you no longer need those extra parentheses. You can get the same result using the new syntax like this: ππ½
print new Person()->getUniqueId();
Personally, I would choose this option anytime. Let me know which one you prefer.
The last update weβll discuss in this article is one that surprised me, and that is... drumroll...π₯π₯π₯
array_find
The array_find
surprised me because I have been writing PHP for about 5 years now, and if you had asked me in my sleep before PHP 8.4 if array_find
was a valid PHP method, I would most likely have said yes π€¦π½ββοΈ, because I just assumed there should be something like that already. Well, as you can tell, I would have been wrong.
So, what does array_find actually do?
array_find
takes an array and a callback, and returns the first element for which the callback returns true
.
If you thought the function would return all the elements where the callback evaluates to true, I'm with you on that, but that's not how it works, so it's important to remember. I'm not surprised, though, because JavaScript uses the same Array.find()
syntax for similar functionality.
Hereβs how to use array_find
in your code: ππ½
$firstTransactionAboveLimit = array_find(
$transactions,
function (Transaction $transaction) {
return $transaction->amount > 5000;
}
);
Pretty neat right π
Itβs a wrap π
In this article, we saw how new features like:
Property hooks: Allow us to attach get
and set
methods directly to a property, removing the need to create separate getter and setter methods.
Asymmetric visibility: Allows properties to have different visibility for reading and writing operations using a single definition, without needing separate getters and setters.
new
without parentheses: This feature lets us create an instance of a class and access its properties or methods without needing to wrap the creation logic in extra parentheses.
array_find
: accepts an array and a callback, returning the first element where the callback evaluates to true
.
Thereβs a lot more where these came from, but I wanted to focus on the ones we can use right away because they are likely to be helpful in our day-to-day work. However, PHP 8.4 includes many other updates, and you can explore them on the official PHP release page.
Thank you !
I sincerely hope that you enjoyed reading this. If you did, kindly drop a thumbs up.
Thanks for sticking with me till the end. If you have any suggestions or feedback, kindly drop them in the comment section. Enjoy the rest of your day...bye π.