dddshelf / ddd-in-php-book-issues

Leave your comments, improvements or book mistakes as an issue! Thanks ❤️
https://leanpub.com/ddd-in-php
28 stars 2 forks source link

using self instead of class name when passing the parameter #79

Closed feyyazesat closed 6 years ago

feyyazesat commented 7 years ago

The documentation stated a point for using self instead of classname as

By using the self keyword, we don’t couple the code with the class name. As such, a change to the class name or namespace won’t affect these factory methods. This small implementation detail helps when refactoring the code at a later date.

class Money
{
    // ...

    public static function fromMoney(Money $aMoney)
    {
        return new self(
            $aMoney->amount(),
            $aMoney->currency()
        );
    }

    public static function ofCurrency(Currency $aCurrency)
    {
        return new self(0, $aCurrency);
    }
}

What about using self on method signature ?

public static function fromMoney(self $aMoney) instead of public static function fromMoney(Money $aMoney)

keyvanakbary commented 6 years ago

Seeing self in type-hints is not very common, that's why we decided to go for the class name. But thanks for your contribution, and sorry for the delay 😓