As a PHP fan, I’m always eager to explore what’s new in each release. With PHP 8.4, set to launch on November 21, 2024, the language is proving it’s far from stagnant. This release introduces features that streamline development, reduce boilerplate, and open new possibilities for elegant, modern coding.
Here’s how I’ll use these features to build my future apps—and why you might love them too!
Property Hooks: Cleaner, Smarter Code
One of my gripes in PHP has been writing repetitive getters and setters for properties.As of 8.4 Property hooks will solve this beautifully by letting you define behavior directly in the property declaration.
Take this example: when a blog author’s name is saved, I might want to sanitize it to remove excess spaces. Property hooks let me do this effortlessly:
class Author
{
public string $name {
set(string $value) => $this->name = trim($value);
get => $this->name;
}
}
Now, every time an author's name is updated, the setter ensures consistency. No need for manual checks sprinkled across the codebase.
For interfaces, this is a game-changer. Imagine ensuring every content model in the blog has standardized property hooks:
interface Content
{
public string $title { get; set; }
public string $slug { get; }
}
This would simplify working with posts, pages, and categories in a unified way.
“New” Without Parentheses: Cleaner Chaining
Chaining methods while creating objects is something I do all the time in blogs—think data transformers or response builders. PHP 8.4 lets you drop unnecessary parentheses, making the code more readable:
$postUrl = new BlogPost($id)->generateSlug()->toUrl();
Compare that to the old way:
$postUrl = (new BlogPost($id))->generateSlug()->toUrl();
It’s a small change, but it declutters the code, especially when working with fluent interfaces or DTOs.
Asymmetric Visibility: Control Over Blog Post Properties
Imagine building a blog where a post’s title is publicly viewable but only editable by the CMS backend. Asymmetric visibility lets us define this level of granularity:
class BlogPost
{
public private(set) string $title;
public private(set) DateTime $publishedAt;
}
This ensures the post’s title can’t be changed outside the class, maintaining data integrity. Similarly, you could make fields writable only to subclasses, perfect for extensible CMS plugins:
class FeaturePost extends BlogPost
{
public protected(set) bool $isFeatured;
}
Array Find: Searching for That Perfect Match
One common task in blogs is searching for a specific tag or category in a list. With array_find(), this becomes much simpler:
$featuredTag = array_find($tags, fn($tag) => $tag->name === 'Featured');
No more fiddling with foreach
loops or relying on third-party helpers. This also works seamlessly with array_any() and array_all() for more complex checks, like ensuring all tags are active:
$allActive = array_all($tags, fn($tag) => $tag->isActive());
HTML5 Parsing: Cleaner Scraping and Rendering
Working with HTML5 in PHP used to mean dealing with quirks in DOMDocument
. Now, the new \Dom\HTMLDocument
simplifies handling HTML strings. For example, parsing a blog comment for validation:
$htmlComment = \Dom\HTMLDocument::createFromString($userComment);
This makes tasks like rendering rich text from a WYSIWYG editor much easier.
Lazy Objects: Optimize Blog Post Loading
Lazy objects are a dream for optimizing performance. Imagine a blog dashboard where you only load post details when necessary:
$initializer = fn() => new BlogPost($id);
$blogPost = (new ReflectionClass(BlogPost::class))->newLazyProxy($initializer);
No need to instantiate every post immediately—just the ones the user interacts with. This is ideal for large blogs with thousands of posts.
Deprecated Implicit Nullable Types: A Nudge Toward Clarity
Implicit nullable types are now deprecated, so instead of this:
function save(Post $post = null) {}
I will now be able to explicitly declare:
function save(?Post $post = null) {}
This will improve type safety, especially when working on large projects. No more surprises when a function behaves differently than expected.
Conclusion
From the powerful property hooks to the array helpers, these features are designed to make life easier for developers. For a blog project—or any application—you’ll find cleaner code, faster execution, and less boilerplate in your day-to-day work.
PHP isn’t just alive; it’s thriving. I can’t wait to see how these features reshape the ecosystem.