My team at Spatie is currenlty building Mailcoach, a solution to self host your e-mail newsletter. Mailcoach can be used a stand alone software or as a Laravel package. Subscribe now at Mailcoach to get a notification as soon as we release it.

The value of the void typehint in PHP

Original – by Freek Van der Herten – 2 minute read

When the void typehint was introduced in PHP 7.1. There was some debate about it. Some people wondered if it is beneficial to type nothing? I was one of them. Meanwhile, I changed my opinion on it. In this short post, I'd like to give you a small example where I think void shines.

This is the example given in the Laravel documentation on local model scopes.

public function scopePopular($query)
{
    return $query->where('votes', '>', 100);
}

That return statement is a lie. Nothing is done with the result.

The given Builder instance in $query is mutable. In PHP, objects are being passed by reference. This means that the $query object in scopePopular is shared with the code that calls scopePopular. By calling where on it, the $query will change both for scopePopular as the code that calls it.

In my projects, I would write the scope above like this:

public function scopePopular(Builder $query): void
{
   $query->where('votes', '>', 100);
}

To me, this is more honest. The void typehint signals to me (and I hope to my team members as well) that there is no point in returning something, and that (in this case) the given parameter likely is mutable.

(Thanks for proofreading this, Brent)

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

You can comment on this post by replying to this tweet.
Ricardo Cabanelas liked on 15th October 2019
Rob Mills liked on 14th October 2019
Stephen Jude retweeted on 14th October 2019
Stephen Jude liked on 14th October 2019
Jino Antony liked on 14th October 2019
Neil Carlo Faisan Sucuangco liked on 13th October 2019
Roman Pronskiy liked on 13th October 2019
Sven liked on 13th October 2019
Pascual Strømsnæs liked on 13th October 2019
Jens Segers replied on 13th October 2019
In legacy projects it's also valuable to see if it's a method that returns nothing, or just a really old method that wasn't converted to PHP 7 type hints yet 😅
Wyatt liked on 12th October 2019
Gaurav Makhecha retweeted on 12th October 2019
Gaurav Makhecha liked on 12th October 2019
Martin Chvila retweeted on 12th October 2019
Martin Chvila liked on 12th October 2019
juliobitencourt liked on 12th October 2019
barthelemy.ehui liked on 12th October 2019
oussama.tn retweeted on 12th October 2019
oussama.tn liked on 12th October 2019
Alessandro Benoit liked on 12th October 2019
ThePHPGuy liked on 12th October 2019
Rizal Fakhri liked on 12th October 2019
Ian Jenkins liked on 12th October 2019
Luka Vavetić liked on 12th October 2019
Audun Rundberg liked on 12th October 2019
Chris Kankiewicz liked on 12th October 2019
ReynerHL liked on 12th October 2019
Daniel Puente liked on 12th October 2019
이현석 Hyunseok Lee liked on 12th October 2019
Matthew Turland retweeted on 12th October 2019
BOUKHLIF Mohamed liked on 12th October 2019
Paul Redmond 🇺🇸 replied on 12th October 2019
This is one of the first good arguments I’ve seen in favor of it. Thanks for the perspective 👌
Mike liked on 12th October 2019
Paul Redmond 🇺🇸 liked on 11th October 2019
Simon Kollross liked on 11th October 2019
Raul retweeted on 11th October 2019
Raul liked on 11th October 2019
Dejan Blazeski liked on 11th October 2019
Freek Van der Herten replied on 11th October 2019
Scopes, like the onein the example, aren’t meant to be chained
Lars Röttig retweeted on 11th October 2019
Marceau Ka 🕉 replied on 11th October 2019
For your example (scope method), I always returns `Illuminate\Database\Eloquent\Builder`, useful for chaining and my IDE.
Mickaël Isaert liked on 11th October 2019
Jérémy DECOOL liked on 11th October 2019
technomike retweeted on 11th October 2019
Travis Fettig retweeted on 11th October 2019
oluwajubelo loves VueJS 🚨 liked on 11th October 2019
Jorge González liked on 11th October 2019
Daniel Werner liked on 11th October 2019
حازم نور ◔̯◔ liked on 11th October 2019
David Cottila retweeted on 11th October 2019
Ryosuke liked on 11th October 2019
Alex Rusin liked on 11th October 2019
Marcel Pociot 🎆 retweeted on 11th October 2019
Tom Schlick liked on 11th October 2019
James Burrow liked on 11th October 2019
ArielSalvadorDev liked on 11th October 2019
Jayden Ireland retweeted on 11th October 2019
pours.io liked on 11th October 2019
pours.io retweeted on 11th October 2019
Julien Tardot replied on 11th October 2019
Sure you can! The scope method defined in your entity is not the one you actually call in a builder context (which involves some `__call` magic) `function scopeWhatever()` vs. `$builder->whatever()`
ArielSalvadorDev retweeted on 11th October 2019
Noe liked on 11th October 2019
Andrej Guran replied on 11th October 2019
This is wrong. You can't chain your builder methods if you return void...
Spatie retweeted on 11th October 2019
Joseph Leedy 🇺🇸 liked on 11th October 2019
Lambert Traccard liked on 11th October 2019
Elie Andraos replied on 11th October 2019
Typo in: “In my projects, I would write the scope above like like this:” Duplicate “like”
Håvard Grimelid liked on 11th October 2019
Rod Elias liked on 11th October 2019
Atish Goswami retweeted on 11th October 2019
Thibault Six liked on 11th October 2019
frinux liked on 11th October 2019
Niels retweeted on 11th October 2019
Burak Çakırel liked on 11th October 2019
ダビッド トレス liked on 11th October 2019
ダビッド トレス retweeted on 11th October 2019
PHP Synopsis retweeted on 11th October 2019
Teun de Kleijne replied on 11th October 2019
That's one of the reasons I like the stuff you come up with. People don't have to use it, don't have to share the same opinion, but you give some extra insights. Really appreciate all effort you put into sharing stuff! 🙌
Peyman Goldasteh 🌀 liked on 11th October 2019
pxgamer retweeted on 11th October 2019
pxgamer liked on 11th October 2019
Maurício M. Fauth 🐘 ⛵ liked on 11th October 2019
Lars Röttig liked on 11th October 2019
James liked on 11th October 2019
Andrés Santibáñez liked on 11th October 2019
Matthew Poulter liked on 11th October 2019
Romain Norberg liked on 11th October 2019
Gert de Pagter liked on 11th October 2019
Scott replied on 11th October 2019
typo, missing the word change I think
Freek Van der Herten replied on 11th October 2019
thanks for bringing this to my attention, fixing
Joost Jacobs liked on 11th October 2019
Vlad Drinceanu liked on 11th October 2019
Tomy liked on 11th October 2019
kapil retweeted on 11th October 2019
HerrAusragend liked on 11th October 2019
Rias Van der Veken liked on 11th October 2019
Galin Denev liked on 11th October 2019
I Can Eat! liked on 11th October 2019
Ahmad Ripaldi liked on 11th October 2019
Ruslan liked on 11th October 2019