Hey Reader, It's almost that time of the year again -- time for Meet Magento Florida! If you are a developer and planning on attending in a couple weeks, you may want to check out my Magento + Xdebug workshop. It's free for attendees, but you'll need a ticket to the conference and attend in-person to see it. Otherwise, below are some new lessons about how around() plugins work, how to implement Magewire in Luma, and a new must-use PHP 8.4 feature.
Create an around plugin in MagentoAround plugins are the most powerful way to modify Magento's behavior. They let you completely wrap existing functions with your own code. We can create a practical example that blocks customer logins from specific email domains. The around plugin has two or more parameters defined:
Within the plugin is where we can place our business logic, which in this lesson is just marking the acount as inactive and incrementing a failure counter. The final step is connecting our plugin to Magento through di.xml, using a descriptive name prefixed with our vendor and module to avoid conflicts. While around plugins are powerful, they should be used sparingly. They can make debugging harder and impact performance, so consider if before/after plugins might work instead.
Create a simple Magewire component in Magento LumaMagewire brings reactivity to Magento without writing any JavaScript -- just PHP! First, you need to install both the magewirephp/magewire and magewirephp/magewire-requirejs packages with Composer. The second one is specifically needed for Luma compatibility. The component itself is straightforward PHP. We just create a simple class in our module’s Magewire directory which contains our logic. This is just a standard PHP class -- nothing crazy. The Magewire component class must then be passed as an argument named “magewire”. This naming is crucial for everything to work properly. The template file is where things get interesting. We can create reactivity just by using HTML attributes, for example "wire:click". And then we can call our PHP function directly with the value for that attribute. When you load up your counter page, each button click will call the PHP function and you get all of the interactivity with just a few lines of PHP - no JavaScript required.
Property hooks in PHP 8.4PHP 8.4 just introduced a game-changing feature called property hooks. Think of it as a way to intercept and validate property values without the traditional getter/setter boilerplate. Before PHP 8.4, we had to create separate getters and setters for any property validation. It worked, but it wasn’t exactly elegant. Now we can define these validations right on the property itself. Just open up some curly braces after your property definition and add your hooks. The syntax is beautifully simple.
You can even use arrow syntax to keep things super clean for simple operations. And the best part? You interact with these properties just like any other public property. No more calling separate getter and setter methods. Just assign values directly to the property, and your validation hooks will catch any issues. It’s a much more natural way to work with class properties while maintaining all the safety of traditional validation.
Tracking down that elusive Magento bug? University members use Maggie Bolt to analyze code, suggest debugging strategies, and explain complex Magento behaviors — available 24/7, no matter where you're stuck.
|
Join 9,000+ developers and get three free video lessons every week.