Laravel Mixins Tips
– liamhammett.com - submitted by Liam Hammett
Some tips when dealing with lots of macros in Laravel, and composing them into mixin classes elegantly.
Read more [liamhammett.com]
Posts tagged with macros
– liamhammett.com - submitted by Liam Hammett
Some tips when dealing with lots of macros in Laravel, and composing them into mixin classes elegantly.
Read more [liamhammett.com]
Just a little reminder that Carbon is macroable in #Laravel 5.5 pic.twitter.com/6l7F883c7N
— Freek Van der Herten (@freekmurze) October 22, 2017
Read more [twitter.com]
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"Freek publishes a super resourceful and practical newsletter. A must for anyone in the Laravel space"
Nicola Malizia wrote a short blog post on how Laravel's handy Macroable trait can be used and how it works under the hood.
If you check the Laravel codebase I’m sure that you can observe that Laravel makes use of traits.There is one trait in the source code that pulls my attention. I’m talking about the Macroable trait. ... The purpose of this trait is to extend (not in an OOP sense) a class at run-time. This way, you can add behavior without editing the original class source code.
https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172
In an awesome article at Sitepoint Christopher Pitt explains how he used the yay macro library to build up plugin framework to add new language features to PHP.
Chris made plugins that allows this syntax in PHP.
// short closure syntax
$items = ["one", "two", "three"];
$ignore = "two";
array_filter($items, ($item) => {
return $item !== $ignore;
});
//class accessors
class Sprocket
{
private $type {
get {
return $this->type;
}
set {
$this->type = $value;
}
unset {
$this->type = "type has been unset";
}
}
}
As with all things, this can be abused. Macros are no exception. This code is definitely not production-ready, though it is conceptually cool.Please don’t be that person who comments about how bad you think the use of this code would be. I’m not actually recommending you use this code, in this form.
Having said that, perhaps you think it’s a cool idea. Can you think of other language features you’d like PHP to get? Maybe you can use the class accessors repository as an example to get you started. Maybe you want to use the plugin repository to automate things, to the point where you can see if your idea has any teeth.
https://www.sitepoint.com/how-to-make-modern-php-more-modern-with-preprocessing/
Check out some more examples on preprocess.io
Very cool stuff.
Laravel's collection class is truly wonderful. It contains a lot of handy methods and you can create some very elegant code with it. In client projects I found myself adding the same macro's over and over again. That's why my colleague Seb and I took some time to create a package aptly called…