Skip to content
Programing

PHP 8.4 Property Hooks: The End of Getters and Setters

Published: Duration: 4:42
0:00 0:00

Transcript

Host

Alex Chan Hey everyone, welcome back to Allur, your home for all things PHP, Laravel, Go, and mobile dev. I’m your host, Alex Chan. Now, if you’ve been writing PHP for—well, basically as long as the language has been around—you know the "ritual." You create a class, you add some private properties, and then you spend the next ten minutes generating getters and setters. `public function getName()`, `public function setName($name)`... it’s the boilerplate we’ve all just accepted as the "cost of doing business" in object-oriented PHP.

Guest

Marco Rossi Thanks so much for having me, Alex! It’s a really exciting time to be talking about PHP. I feel like every year people say "PHP is dead," and then the core team drops something like Property Hooks and we all realize the language is actually evolving faster than almost anything else out there.

Host

Alex Chan Right? It’s definitely not the PHP 4 or 5 we remember. So, let’s jump straight into it. Property Hooks. When I first saw the RFC, my first thought was, "Oh, this looks like C# or Swift." For someone who hasn't seen the code yet, how would you describe what a Property Hook actually *is*?

Guest

Marco Rossi You hit the nail on the head with the C# comparison. Essentially, Property Hooks allow us to define the "get" and "set" logic directly *inside* the property declaration. Instead of having a private property and a separate public method to interact with it, you just define the property as public—or whatever visibility you want—and then attach these hooks.

Host

Alex Chan I love that. It feels much more "first-class." I was looking at an example earlier where someone was using a `get` hook to automatically format a string. Could you walk us through what that syntax looks like? Because it’s a bit of a departure from what we’re used to in PHP.

Guest

Marco Rossi Yeah, it’s a bit of a "whoa" moment when you first see it. Imagine you have `public string $name`. In 8.4, you’d follow that with braces. Inside, you’d write `get => strtoupper($this->name);`. That fat arrow—the `=>`—is very familiar to us from arrow functions.

Host

Alex Chan Wait, so actually... I have a question about that. If I'm using `$this->name` inside the hook for `$name`, does that create some kind of infinite loop? Like, is the hook calling itself?

Guest

Marco Rossi

Host

Alex Chan Oh, that makes sense! That’s a relief. Honestly, I’ve lost count of how many times I’ve accidentally created loops in other languages trying to do something similar.

Guest

Marco Rossi It’s definitely more than just sugar. I mean, think about a typical Data Transfer Object—a DTO. Or even a simple Eloquent-style model. If you have ten properties and you want to ensure data integrity for all of them, you’re looking at maybe 60 to 80 lines of just getters and setters. It makes the file huge, it’s harder to read, and—this is the big one—it’s harder to maintain.

Host

Alex Chan That’s a great point. It’s about colocation. Keeping the logic where the data lives.

Guest

Marco Rossi This is actually my favorite part. You can define a property that doesn't actually store anything. Like, let's say you have `$firstName` and `$lastName`. You can define a `public string $fullName` property that *only* has a `get` hook. That hook just returns `$this->firstName . ' ' . $this->lastName`.

Host

Alex Chan Interesting! So, it really blurs the line between a property and a method, but in a way that feels very natural for the consumer of the class.

Guest

Marco Rossi Oh, absolutely. It’s the "with great power comes great responsibility" thing. Just because you *can* put 20 lines of logic in a hook doesn't mean you *should*. If a hook gets too complex, it’s usually a sign that it should probably just be a method.

Host

Alex Chan Right, because when you see `$user->name`, you expect it to be fast. You don’t expect it to trigger a five-second API call!

Guest

Marco Rossi

Host

Alex Chan I hadn't even thought about the tooling! Yeah, a "Refactor to Property Hook" button in PHPStorm would be incredible.

Guest

Marco Rossi The best place is definitely the official PHP RFC page for Property Hooks. It’s surprisingly readable and has a ton of code samples. Also, keep an eye on the Laravel and Symfony blogs—those communities are already prepping for how this will change their frameworks. And of course, just spin up a Docker container with the 8.4 nightly build and break things! That’s the best way to learn.

Host

Alex Chan "Spin it up and break things"—the developer's mantra! Marco, thank you so much for joining us on Allur today. This was awesome.

Guest

Marco Rossi My pleasure, Alex. Thanks for having me!

Host

Alex Chan And thank you all for tuning in! If you’re excited about PHP 8.4 or if you’re worried that Property Hooks are going to make your code too "magic," we want to hear about it. Hit us up on social media or join our Discord.

Tags

web development backend php modernization oop clean code