💎 Hidden gem in PHP 8.3... a game-changer for Magento devs?


I hope everyone is having a great summer and able to take some time off! After a few meh summers, we've been enjoying some 90+ degree days in Cleveland, Ohio, and it's wonderful.

I always see summer as a bit of a break from the normal coding routine, a chance to get outside of our heads and clear my mind -- a bit like starting a new year.

Anyone relate?

This Segment is about a new feature available in PHP 8.3. But first, a word from our sponsor:

Sponsored by Portless

Skip the ship, unlock cashflow and scale globally in less time

Discover the future of e-commerce logistics with Portless, your innovative 3PL partner. Portless revolutionizes order fulfillment with lightning-fast direct shipping from China—deliveries in as little as 6 days. Improve lead times by up to 10x and boost cash flow by up to 3x without logistical headaches. Enjoy local tracking and exceptional customer support for a fully American shopping experience. Start your global expansion now with 20% off pick-and-pack fees for your first three months. Elevate your e-commerce game with Portless!

🕵️‍♂️ Using the PHP 8.3 Override Attribute in Magento

As Magento developers, we're always on the lookout for tools and techniques that can make our code more robust and easier to maintain. With PHP 8.3, we've been gifted a new feature that does just that: the #[\Override] attribute.

You might be familiar with PHP attributes already. They're those little tags wrapped in square brackets with an @ symbol, sitting above classes, methods, or properties. This provides extra info about your code without changing its execution.

In fact, you've probably encountered attributes in Magento without even realizing it. For instance, there's a #[\ReturnTypeWillChange] attribute in the Magento\Framework\App\RouterList class, and a #[\DataFixture] attribute in Magento unit tests.

Now, PHP 8.3 has introduced a new attribute that I believe will significantly impact how we handle class extensions and customizations in Magento: #[\Override].

A Practical Example of #[Override] in Magento

To understand how the #[\Override] attribute works in practice, let's look at an example that is specific to Magento.

Let's say that we are extending Magento's JSON serializer to add some custom functionality:


declare(strict_types=1);

namespace Macademy\DebugBooster\Serialize\Serializer;

use Magento\Framework\Serialize\Serializer\Json as BaseJson;

class Json extends BaseJson
{
    /**
     * Serialize data into string
     *
     * @param mixed $data
     * @return string
     * @throws \InvalidArgumentException|\JsonException
     */
    #[\Override]
    public function serialize($data): string
    {
        $result = json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
        if (false === $result) {
            throw new \InvalidArgumentException("Unable to serialize value. Error: " . json_last_error_msg());
        }
        return $result;
    }

    /**
     * Unserialize the given string
     *
     * @param string $string
     * @return mixed
     * @throws \InvalidArgumentException
     */
    #[\Override]
    public function unserialize($string): mixed
    {
        try {
            $result = json_decode($string, null, 512, JSON_THROW_ON_ERROR);
        } catch (\JsonException $e) {
            throw new \InvalidArgumentException("Unable to unserialize value. Error: " . $e->getMessage());
        }
        return $result;
    }
}

In this code, we’re using the #[\Override] attribute to explicitly indicate that we intend to override the serialize() and unserialize() methods from the parent class.

Let’s focus on the serialize() method.

We’ve added two new flags to the json_encode function:

  1. JSON_THROW_ON_ERROR: This ensures that any encoding errors will throw an exception.
  2. JSON_UNESCAPED_SLASHES: This improves the readability of our JSON output by preventing the escaping of slashes.

These changes throw better warnings when JSON is encoded, which creates more stable and predictable behavior.

Now, here’s where the #[\Override] attribute comes into play...

💫 M.academy updates

We are going strong on enhancements to M.academy in the back half of this year. So much so, that I think this will be the most transformative 6 months that we'll ever have.

A few things coming up include:

  • The Adobe Commerce Developer Certification Prep Course, along with an enhanced exam-taking portal which will efficiently train you on passing the exam
  • The highly-demanded Getting Started with Hyvä course
  • Maggie Bolt 2.0

There are also quite a few other things that we're working on behind the scenes that I'm so excited to share with you, but I'll leave them as a surprise for when they happen.


Once & done deal

As you know, M.academy no longer offers discounts on the University plan other than purchasing power parity promos, which helps make it more affordable for those living in countries with bad exchange rates.

But Reader, since you previously enrolled in the University, I wanted to give you one last chance to lock-in a special rate for being a previous M.academy student 🤗

For the next 4 days only, I wanted to extend you 10% off the University. This includes both the individual yearly plan and individual forever options. You can even combine a PPP code on top of this special rate if you live in one of those supported countries.

You'll also be locked in this rate forever, so even if the plan price goes up in the future, you won't need to worry about it. But these special links will only be good through Saturday:

My mission is to install confidence in you to efficiently master Magento, saving you time and distress with learning this complex framework. Let's get back to it and do this 💪


A question for you:

When have you felt most energized, creative, or "in the zone" while working on a Magento project, and what were you doing?


Join Segment, the Magento newsletter

Join Segment, M.academy's free monthly newsletter about Magento, read my thousands of developers & merchants to keep in touch with the industry. Check out previous issues of Segment by clicking the Posts tab below 👇

Read more from Join Segment, the Magento newsletter
M.academy Website Home Page

Aug 2024 Read by 9,000+ Magento developers.Send to a friend. Complete M.academy refresh Hey friend, It's been a few years since M.academy launched, and I thought it was time that our image got a bit of a refresh. So over the summer, I worked on a few things: A refreshed website and updated tagline: Lots of energy, bright colors, and a new focus on "mastering" Magento development rather than simply learning the fundamentals -- since there are already courses for all of those 😉 An updated look...

This hasn't been a quiet week by any means, even in Magento land. Before we get to the guts of this issue, which is all about how to debug and troubleshoot product collection issues, I need to let you know about urgent security exploit that needs to be addressed. But first, a word from our sponsor. If you are interested in order fulfillment, check out Portless: Sponsored by Portless Skip the ship, unlock cashflow and scale globally in less time Discover the future of e-commerce logistics with...

Redis adopts dual-source licensing

There was a big of drama with Redis the last few weeks regarding updates to its licensing, Magento 2.4.7 was released, and we got some numbers released to us about Hyvä installations. Let's get to it 😄 🥳 What's new in Magento 2.4.7 I always cover what's new and interesting with new Magento releases, and 2.4.7 was just released last week. If you want to know everything notable in this release (and skip all the boring stuff), check out my newest video which will inform you about anything new in...