Mailcoach is a self-hosted email list manager. It integrates with services like Amazon SES, Mailgun or Sendgrid to send out mailings affordably.

If you're using Laravel, you can learn the techniques we used to create Mailcoach in your own projects by watching the Mailcoach video course.

📺 How to avoid large function signatures by using pending objects

Original – by Freek Van der Herten – 1 minute read

In this video I demonstrate how to you can create a fluent API by using pending objects. We'll take a look at how subscribers can be created in Mailcoach. As a bonus I explain how jobs are dispatched using a pending object in Laravel.

This video is part of the Mailcoach video course. In the video course I explain how we built Mailcoach and give tips on how to write well structured and clean code.

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.
Richard Radermacher liked on 19th February 2020
Stefan Zweifel liked on 19th February 2020
Gilbert Pellegrom liked on 19th February 2020
Sam Carré 🇬🇧 liked on 19th February 2020
Fahmi Z Siregar liked on 19th February 2020
Richard Ottinger liked on 19th February 2020
Christoph Rumpel 🤠 liked on 19th February 2020
Roberto De la Fuente liked on 19th February 2020
Alexandru Stratulat liked on 18th February 2020
Chin Leung liked on 18th February 2020
Alexis Rojas liked on 18th February 2020
Manuel Teuber liked on 18th February 2020
Full Help liked on 18th February 2020
.: PoOYaN :. liked on 18th February 2020
Popescu Tudor liked on 18th February 2020
Adam Tesh liked on 18th February 2020
rabbl liked on 18th February 2020
Romain Norberg replied on 18th February 2020
thank you! I will test this in a future project
Cristian Tăbăcitu liked on 18th February 2020
Mark Jaquith liked on 18th February 2020
Koen Hendriks liked on 18th February 2020
hoangbkit liked on 18th February 2020
Rodrigo E. M. Souza liked on 18th February 2020
Mehmed H liked on 18th February 2020
Murali ⌘ Krishna liked on 18th February 2020
Erik Larsson liked on 18th February 2020
Khai Rahman liked on 18th February 2020
George Drakakis 🖖 liked on 18th February 2020
mira kovar liked on 17th February 2020
Leonel Elimpe liked on 17th February 2020
Arputharaj liked on 17th February 2020
Stijn Vanouplines liked on 17th February 2020
Felipe Dalcin liked on 17th February 2020
이현석 Hyunseok Lee liked on 17th February 2020
alireza faghani liked on 17th February 2020
Ivan Radunovic liked on 17th February 2020
Motorhome Fulltimer liked on 17th February 2020
Claudson Martins liked on 17th February 2020
Neil Carlo Faisan Sucuangco liked on 17th February 2020
Cyril de Wit liked on 17th February 2020
Feras Shaer 🇵🇸 liked on 17th February 2020
Adam 🤙🏼 Mench liked on 17th February 2020
Hardik Shah liked on 17th February 2020
Adam 🤙🏼 Mench replied on 17th February 2020
@freekmurze and not to forget @taylorotwell this is genius. Of course I have consumed this type of fluent code, but never constructed a pending object pattern class before! Looking forward to trying it out!
Flamur Mavraj liked on 17th February 2020
Mak Man liked on 17th February 2020
Muhammad Imran liked on 17th February 2020
Semyon Chetvertnyh liked on 17th February 2020
MarcoDM liked on 17th February 2020
Osama Kamel liked on 17th February 2020
Scorp974 liked on 17th February 2020
Mark Beech liked on 17th February 2020
José Cage liked on 17th February 2020
Rich liked on 17th February 2020
happy★coder liked on 17th February 2020
Luca Degasperi liked on 17th February 2020
eminiarts liked on 17th February 2020
Adrian Crisan liked on 17th February 2020
Ξlliott Lawson liked on 17th February 2020
Martin Carlin liked on 17th February 2020
David Strada liked on 17th February 2020
Jiří Žižka liked on 17th February 2020
Kalle Palokankare liked on 17th February 2020
Julien Cauvin liked on 17th February 2020
Mark de Heij liked on 17th February 2020
traviselkins liked on 17th February 2020
Efrain Salas liked on 17th February 2020
Daniele Rosario liked on 17th February 2020
Romain Norberg liked on 17th February 2020
Ishan Vyas retweeted on 17th February 2020
NUNO MADURO 🅱 retweeted on 17th February 2020
Paul Redmond 🇺🇸 retweeted on 17th February 2020
seschi98 liked on 17th February 2020
Christophe Vuagniaux liked on 17th February 2020
Ishan Vyas liked on 17th February 2020
Erick Patrick liked on 17th February 2020
Thomas Bailly liked on 17th February 2020
Monney Arthur liked on 17th February 2020
Prashank Abhishek liked on 17th February 2020
Alejandro Vásquez N. liked on 17th February 2020
NUNO MADURO 🅱 replied on 17th February 2020
Elegant. 👏🏻
George liked on 17th February 2020
Nick retweeted on 17th February 2020
PHP Synopsis retweeted on 17th February 2020
Ruslan liked on 17th February 2020
Ross Bookmarks liked on 17th February 2020
Filipe Danielski liked on 17th February 2020
Odinn Adalsteinsson liked on 17th February 2020
Agasi Gilang Persada liked on 17th February 2020
mr flash liked on 17th February 2020
Dave Liddament liked on 17th February 2020
Raphael Cunha liked on 17th February 2020
M. Vug liked on 17th February 2020
Wyatt liked on 17th February 2020
Dave Liddament replied on 17th February 2020
Thanks. Also thanks for the video. Interesting to see use of destructor. Am I right in thinking they are called the moment the final reference to the object is removed? Or is it when the object is garbage collected?
Freek Van der Herten replied on 17th February 2020
Mostly I use something that sound nice and logical in the context where I use this pattern.
Dave Liddament replied on 17th February 2020
Interesting. Looks like builder pattern. One benefit I see of the builder pattern is that, by convention, the final (or finishing) method is build(). How do you know which is the 'finishing method' is for the pending object, without have to look at all method return types?
Miguel Piedrafita 🚀 liked on 17th February 2020
Suraj Adsul retweeted on 17th February 2020
Syl retweeted on 17th February 2020
Spatie retweeted on 17th February 2020
warren liked on 17th February 2020
/dev/glen liked on 17th February 2020
Suraj Adsul liked on 17th February 2020
Antti Peisa liked on 17th February 2020
Syl liked on 17th February 2020
Goran Galinec liked on 17th February 2020
Jake Casto liked on 17th February 2020
Anton Kristensen liked on 17th February 2020
Cristian liked on 17th February 2020
Ben Sampson liked on 17th February 2020
Richard liked on 17th February 2020
Nikita Tolkachev replied on 17th February 2020
It's just fancy name for classic Builder pattern.
Lawrence Enehizena replied on 17th February 2020
Nice tip. But I am a bit confused. Is there any difference between this and method chaining? Or is “pending object” another name for method chaining?
Sanket Raut retweeted on 17th February 2020
Steve Bauman liked on 17th February 2020
NUNO MADURO 🅱 liked on 17th February 2020
Ray Meibaum 👨🏻‍💻 liked on 17th February 2020
Matthew Poulter liked on 3rd January 2020
DevCamp retweeted on 1st January 2020
DevCamp liked on 1st January 2020
Claudson Martins replied on 1st January 2020
It could be done returning the model it self, instead of a PendingModel. But I think the goal behind the creation of a pending object is to avoid things that could lead to mistakes. Like reach for a relationship when the model itself isn't persisted yet. Am I right?
Claudson Martins replied on 1st January 2020
Those videos are great, but if you don't mind, could you enable the video speed option? I like to watch everything in 2x 😁
Johan Alvarez liked on 1st January 2020
Manojkiran liked on 31st December 2019
Sohail Ahmad retweeted on 31st December 2019
Sohail Ahmad liked on 31st December 2019
Luca Malattia liked on 31st December 2019
Lukas Cornille liked on 31st December 2019
Marcin liked on 31st December 2019
David Franco liked on 31st December 2019
Abdulrahman As'ad liked on 31st December 2019
Linka Softwares liked on 31st December 2019
Khai Rahman retweeted on 31st December 2019
Khai Rahman liked on 31st December 2019
~/αhmєd liked on 31st December 2019
Misbah ansori liked on 31st December 2019
Dominik Kowalski liked on 31st December 2019
Devon Mather liked on 31st December 2019
Freek Van der Herten replied on 31st December 2019
Thanks!
JΞRRΞD liked on 31st December 2019
Jan-Joost Heurter replied on 31st December 2019
I see some great improvements coming up in 2020 🙌 Great explanation and a great video 👌
James Hemery liked on 31st December 2019
Maurizio retweeted on 31st December 2019
Percy Astocaza liked on 31st December 2019
Jan-Joost Heurter liked on 31st December 2019
Maurizio liked on 31st December 2019
oluwajubelo loves VueJS 🚨 liked on 30th December 2019
Emanuel liked on 30th December 2019
Joan Morell liked on 30th December 2019
DiegoGermánGonzález retweeted on 30th December 2019
Richard Jeffery retweeted on 30th December 2019
Sebastian Rasch liked on 30th December 2019
Adib Hanna retweeted on 30th December 2019
Rolf den Hartog liked on 30th December 2019
rik 🔝 liked on 30th December 2019
Fred Carlsen liked on 30th December 2019
Patrick Artounian liked on 30th December 2019
Alexander von Studnitz liked on 30th December 2019
Mickaël Isaert liked on 30th December 2019
Murali ⌘ Krishna liked on 30th December 2019
Wyatt liked on 30th December 2019
Miguel Piedrafita 🚀 liked on 30th December 2019
Craig Paul retweeted on 30th December 2019
Mozammil liked on 30th December 2019
Chris Smiles liked on 30th December 2019
Matt Murtaugh liked on 30th December 2019
Craig Paul liked on 30th December 2019
loreias liked on 30th December 2019
Kalle Palokankare liked on 30th December 2019
Tim Spratt liked on 30th December 2019
Parthasarathi G K liked on 30th December 2019
Mazedul Islam Khan liked on 30th December 2019
Cyril de Wit liked on 30th December 2019
Joseph Silber replied on 30th December 2019
Just beware: this breaks when doing it in Tinker, since Tinker keeps a reference to the object.
undefined liked on 30th December 2019
Florian Wartner liked on 30th December 2019
José Cage liked on 30th December 2019
oluwajubelo loves VueJS 🚨 retweeted on 30th December 2019
Tomáš Máčala liked on 30th December 2019
PHP Synopsis retweeted on 30th December 2019
Osmar Alves liked on 30th December 2019
Yannick Yayo liked on 30th December 2019
Matan Yadaev liked on 30th December 2019
Roni Estein liked on 30th December 2019
Andre Sayej liked on 30th December 2019
alan wilson liked on 30th December 2019
Tom Witkowski liked on 30th December 2019
이현석 Hyunseok Lee liked on 30th December 2019
Matan Yadaev replied on 30th December 2019
Cool, thanks. Is that pending object the correct name for this pattern? I didn't find other articles about this pattern.
Narayan Ramchandani liked on 30th December 2019
Roni Estein replied on 30th December 2019
Thanks, besides being awesome, taking the time to produce posts and videos on awesome tips is what makes you stand apart.
bernard kssy retweeted on 30th December 2019
Victor Abbah Nkoms retweeted on 30th December 2019
Jimmy Lipham liked on 30th December 2019
Lars Klein liked on 30th December 2019
Victor Abbah Nkoms liked on 30th December 2019
Jeffrey van Rossum liked on 30th December 2019
Arash liked on 30th December 2019
Rati Wannapanop liked on 30th December 2019