Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 679 Bytes

named_constructor.md

File metadata and controls

21 lines (18 loc) · 679 Bytes

Named constructor (static factory methods)

Named constructors can add clarity to your code. With the following example, the constructor params may not be obvious without actually digging into the source. A named constructor allow us to sidestep any doubt during setup

class Product
{
    // The constructor can become private in this case
    private function __construct($category, $price = null)
    {
        $this->category = $category;
        $this->price = $price;
    }

    public static function withCategoryAndPrice($category, $price)
    {
        return new static($category, $price);
    }
}

$product  = Product::withCategoryAndPrice('clothes', 50);