Evolution Note: This post was written in May 2019 and reviewed in October 2026. This concluding part of the PHP guitar series explores information hiding and domain contracts in object-oriented design. For current technical writings on system architecture, visit the homepage.

In the previous post, we refactored our script to encapsulate musical notations inside a dedicated Note class.

While our script functioned correctly, comparing songs still relied on PHP’s loose equality operator (==). Relying on default equality checking reveals an architectural weakness as a codebase evolves.

The Problem with Operator Overloading and Magic Equality

Using double equality like $song == $anotherSong presents subtle maintainability issues:

$song == $anotherSong;

When PHP evaluates == between two object instances, it performs a comparison across all object properties. If we later add an internal cache property or timestamp to the Song class, two matching melodies will suddenly evaluate to false because their internal cache states differ.

Callers should not need to inspect internal class properties to know whether comparison works.

Encapsulating Comparison with Explicit Methods

A cleaner object-oriented design replaces implicit operators with intent-revealing methods:

$song->isSameWith($anotherSong);

Replacing raw operators with an explicit method makes code expressive by stating intent directly. It also hides internal fields from external callers, allowing you to alter comparison rules, such as comparing pitches while ignoring tempo metadata, without breaking existing call sites.

Implementing Information Hiding in Note and Song

We implement equality methods on both the Note and Song classes:

class Note
{
    public $note;

    public function __construct(string $note)
    {
        $this->note = $note;
    }

    public function isSameWith(Note $anotherNote): bool
    {
        return $this->note === $anotherNote->note;
    }
}

Next, the Song class delegates pitch comparison down to individual notes:

class Song
{
    public $notes = [];

    public function __construct(array $notes)
    {
        $this->notes = $notes;
    }

    public function isSameWith(Song $anotherSong): bool
    {
        if (count($this->notes) !== count($anotherSong->notes)) {
            return false;
        }

        foreach ($this->notes as $index => $note) {
            if (!$note->isSameWith($anotherSong->notes[$index])) {
                return false;
            }
        }

        return true;
    }
}

Now we update our verification routine:

function printWhetherSongsAreEqual(Song $song, Song $anotherSong)
{
    if ($song->isSameWith($anotherSong)) {
        echo 'The two songs are the same.' . PHP_EOL;
    } else {
        echo 'The two songs are NOT the same.' . PHP_EOL;
    }
}

Executing this in the terminal confirms that comparing Romance and English notations works predictably through explicit domain methods.

Takeaway: Intent-Revealing Domain Interfaces

What began as a playful script to play guitar chords from the command line illustrates an enduring software design lesson. Moving from raw primitive arrays toward encapsulated value objects produces code that is easier to test and maintain over time.

You can inspect the complete final script on GitLab Snippets. If you enjoy discussing domain-driven design or software modeling, feel free to reach out via Email.