Evolution Note: This post was written in May 2019 and reviewed in October 2026. This is Part Two of my PHP guitar series, focusing on refactoring. Moving from an initial script toward domain modeling and encapsulation remains a foundational engineering exercise. For current technical publications, visit the homepage.

Let’s compose and play another melody with our script from the previous post.

$anotherSong = new Song(['Sol', 'Sol', 'Sol', 'Mi', 'Fa', 'Fa', 'Fa', 'Re']);
$guitar->play($song);

The Limitations of Comparing Raw String Arrays

It sounds like the same song as the first one. We can compare them using the double equality operator ==.

if ($song == $anotherSong) {
	echo 'The two songs are the same' . PHP_EOL;
} else {
	echo 'The two songs are not the same' . PHP_EOL;
}

When we run the script, PHP confirms that both songs match because their internal arrays contain identical string values. Now let’s compose a third song using English pitch names:

$beethovenFifthSymphony = new Song(['G', 'G', 'G', 'E', 'F', 'F', 'F', 'D']);

To avoid repeating comparison logic, we introduce a helper function:

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

And then compare all three songs:

$notes = ['Sol', 'Sol', 'Sol', 'Mi', 'Fa', 'Fa', 'Fa', 'Re'];
$song = new Song($notes);

$anotherSong = new Song(['Sol', 'Sol', 'Sol', 'Mi', 'Fa', 'Fa', 'Fa', 'Re']);
echo 'Is $song equal with $anotherSong?' . PHP_EOL;
printWhetherSongsAreEqual($song, $anotherSong);

$beethovenFifthSymphony = new Song(['G', 'G', 'G', 'E', 'F', 'F', 'F', 'D']);
echo 'Is $song equal with $beethovenFifthSymphony?' . PHP_EOL;
printWhetherSongsAreEqual($song, $beethovenFifthSymphony);

When executed in the console:

[savvas@localhost play_guitar_with_php]$ php play_guitar_second.php
Is $song equal with $anotherSong?
The two songs are the same.
Is $song equal with $beethovenFifthSymphony?
The two songs are NOT the same.

Musical Notations: Solfège vs. English Pitch Names

Musically, the two melodies are identical. The difference is cultural notation: Romance solfège (Sol, Mi, Fa, Re) versus English letter pitch (G, E, F, D).

Because PHP compares array elements with literal string comparison, it reports them as unequal. To resolve this, we encapsulate pitch identity inside a dedicated Note model.

class Note
{
	public $note;

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

Encapsulating Pitch Mapping with Named Constructors

If we compare new Note('Sol') == new Note('G'), PHP still evaluates this to false.

We can solve this by normalizing incoming notations to an internal canonical representation. Choosing English notation as the internal standard, we provide static named constructor methods:

class Note
{
    const FROM_ROMANCE_TO_ENGLISH = [
        'Do' => 'C',
        'Re' => 'D',
        'Mi' => 'E',
        'Fa' => 'F',
        'Sol' => 'G',
        'La' => 'A',
        'Si' => 'B'
    ];

    public $note;

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

    public static function fromRomanceNotation(string $romanceNotationNote)
    {
        return new static(self::FROM_ROMANCE_TO_ENGLISH[$romanceNotationNote]);
    }

    public static function fromEnglishNotation(string $englishNotationNote)
    {
        return new static($englishNotationNote);
    }
}

Static factory methods allow callers to instantiate notes with explicit domain intent without knowing the internal canonical representation.

Enforcing Invariants and Protecting Internal State

$do = new Note('Do');
// The internal notation is the English, but we parsed to it Romance notation
$do->note = 'Re';
// We are assigning directly to the internal property the note.
$re = Note::fromRomanceNotation('D');
// We are using the English notation to create from Romance notation.
$mi = Note::fromEnglishNotation('mi');
// And the opposite.

To prevent those issues we will do the followings

  1. Switch the visibility of the constructor to private so only the internal methods of the class will be able to use it.
  2. Switch the visibility of the property $note to private, so it can be change only from the class.
  3. Allow only the romance notation strings for the Romance Notation constructor and the English notation for the English Notation constructor.
class Note
{
    const ENGLISH_NOTATION_NOTES = [
        'C', 'D', 'E', 'F', 'G', 'A', 'B'
    ];

    const ROMANCE_NOTATION_NOTES = [
        'Do', 'Re', 'Mi', 'Fa', 'Sol', 'La', 'Si'
    ];

    const FROM_ROMANCE_TO_ENGLISH = [
        'Do' => 'C',
        'Re' => 'D',
        'Mi' => 'E',
        'Fa' => 'F',
        'Sol' => 'G',
        'La' => 'A',
        'Si' => 'B'
    ];

    private $note;

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

    public static function fromRomanceNotation(string $romanceNotationNote)
    {
        if (!in_array($romanceNotationNote, self::ROMANCE_NOTATION_NOTES)) {
            throw new \Exception('Invalid arguments for the Romance Notation constructor.');
        }
        return new static(self::FROM_ROMANCE_TO_ENGLISH[$romanceNotationNote]);
    }

    public static function fromEnglishNotation(string $englishNotationNote)
    {
        if (!in_array($englishNotationNote, self::ENGLISH_NOTATION_NOTES)) {
            throw new \Exception('Invalid arguments for the English Notation constructor.');
        }

        return new static($englishNotationNote);
    }
}

Note here that we have used constants, exceptions and the in_array function.

Now we can improve also the Song class, so we will be able to construct songs from array of both notations.

class Song
{
    private $notes = [];

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

    public static function fromEnglishNotation(array $arrayEnglishNotationNotes)
    {
        $arrayNotes = [];
        foreach($arrayEnglishNotationNotes as $stringEnglishNotationNote) {
            $arrayNotes[] = Note::fromEnglishNotationString($stringEnglishNotationNote);
        }
        return new static($arrayNotes);
    }

    public static function fromRomanceNotation(array $arrayRomanceNotationNotes)
    {
        $arrayNotes = [];
        foreach($arrayRomanceNotationNotes as $stringRomanceNotationNote) {
            $arrayNotes[] = Note::fromRomanceNotationString($stringRomanceNotationNote);
        }
        return new static($arrayNotes);
    }
}

And then rewrite our test script.

$notes = ['Sol', 'Sol', 'Sol', 'Mi', 'Fa', 'Fa', 'Fa', 'Re'];
$song = Song::fromRomanceNotation($notes);

$anotherSong = Song::fromRomanceNotation(['Sol', 'Sol', 'Sol', 'Mi', 'Fa', 'Fa', 'Fa', 'Re']);
echo 'Is $song equal with $anotherSong?' . PHP_EOL;
printWhetherSongsAreEqual($song, $anotherSong);

$beethovenFifthSymphony = Song::fromEnglishNotation(['G', 'G', 'G', 'E', 'F', 'F', 'F', 'D']);
echo 'Is $song equal with $beethovenFifthSympony?' . PHP_EOL;
printWhetherSongsAreEqual($song, $beethovenFifthSymphony);

When we run again the script, on both cases the songs will be equal.

[savvas@localhost play_guitar_with_php]$ php play_guitar_second.php
Is $song equal with $anotherSong?
The two songs are the same.
Is $song equal with $beethovenFifthSympony?
The two songs are the same.

That’s all for today.

Copy the Script 🔓 and Play with it ⛹.

Be Creative 🎨 and feel free to connect via Email 📧.