A constructor can provide defaults so object creation stays convenient.

optional constructor input Default constructor values let a class support both explicit and ordinary setup.

Constructor Defaults

rate
constructor_defaults.php
Replay: real traced execution (multi-file project)
<?php
class ShippingBox
{
    public function __construct(private int $weight = 1)
    {
    }

    public function cost(int $rate): int
    {
        return $this->weight * $rate;
    }
}

$rate = 4;
$defaultCost = (new ShippingBox())->cost($rate);
$heavyCost = (new ShippingBox(3))->cost($rate);

echo "rate=" . $rate . "\n";
echo "defaultCost=" . $defaultCost . "\n";
echo "heavyCost=" . $heavyCost . "\n";
<?php
class ShippingBox
{
    public function __construct(private int $weight = 1)
    {
    }

    public function cost(int $rate): int
    {
        return $this->weight * $rate;
    }
}

$rate = 2;
$defaultCost = (new ShippingBox())->cost($rate);
$heavyCost = (new ShippingBox(3))->cost($rate);

echo "rate=" . $rate . "\n";
echo "defaultCost=" . $defaultCost . "\n";
echo "heavyCost=" . $heavyCost . "\n";
<?php
class ShippingBox
{
    public function __construct(private int $weight = 1)
    {
    }

    public function cost(int $rate): int
    {
        return $this->weight * $rate;
    }
}

$rate = 6;
$defaultCost = (new ShippingBox())->cost($rate);
$heavyCost = (new ShippingBox(3))->cost($rate);

echo "rate=" . $rate . "\n";
echo "defaultCost=" . $defaultCost . "\n";
echo "heavyCost=" . $heavyCost . "\n";
  1. $rate ← 4

    14$rate→ 4 = 4; //@rate=2, 615$defaultCost = (new ShippingBox())->cost($rate4);16$heavyCost = (new ShippingBox(3))->cost($rate);
  2. public function __construct(private int $weight = 1)

    pass 1 of 2
    3{4    public function __construct(private int $weight1 = 1)5    {6    }
  3. public function cost(int $rate): int

    pass 1 of 2
    8public function cost(int $rate4): int9{10    return $this->weight1 * $rate4;11}
  4. $defaultCost ← 4

    14$rate = 4; //@rate=2, 615$defaultCost→ 4 = (new ShippingBox())->cost($rate4);16$heavyCost = (new ShippingBox(3))->cost($rate4);
  5. public function __construct(private int $weight = 1)

    pass 2 of 2
    3{4    public function __construct(private int $weight3 = 1)5    {6    }
  6. public function cost(int $rate): int

    pass 2 of 2
    8public function cost(int $rate4): int9{10    return $this->weight3 * $rate4;11}
  7. $heavyCost ← 12

    15$defaultCost = (new ShippingBox())->cost($rate);16$heavyCost→ 12 = (new ShippingBox(3))->cost($rate4);1718echo "rate=" . $rate4 . "\n";19echo "defaultCost=" . $defaultCost4 . "\n";20echo "heavyCost=" . $heavyCost12 . "\n";
    outputrate=4
    defaultCost=4
    heavyCost=12
  1. $rate ← 2

    14$rate→ 2 = 2;15$defaultCost = (new ShippingBox())->cost($rate2);16$heavyCost = (new ShippingBox(3))->cost($rate);
  2. public function __construct(private int $weight = 1)

    pass 1 of 2
    3{4    public function __construct(private int $weight1 = 1)5    {6    }
  3. public function cost(int $rate): int

    pass 1 of 2
    8public function cost(int $rate2): int9{10    return $this->weight1 * $rate2;11}
  4. $defaultCost ← 2

    14$rate = 2;15$defaultCost→ 2 = (new ShippingBox())->cost($rate2);16$heavyCost = (new ShippingBox(3))->cost($rate2);
  5. public function __construct(private int $weight = 1)

    pass 2 of 2
    3{4    public function __construct(private int $weight3 = 1)5    {6    }
  6. public function cost(int $rate): int

    pass 2 of 2
    8public function cost(int $rate2): int9{10    return $this->weight3 * $rate2;11}
  7. $heavyCost ← 6

    15$defaultCost = (new ShippingBox())->cost($rate);16$heavyCost→ 6 = (new ShippingBox(3))->cost($rate2);1718echo "rate=" . $rate2 . "\n";19echo "defaultCost=" . $defaultCost2 . "\n";20echo "heavyCost=" . $heavyCost6 . "\n";
    outputrate=2
    defaultCost=2
    heavyCost=6
  1. $rate ← 6

    14$rate→ 6 = 6;15$defaultCost = (new ShippingBox())->cost($rate6);16$heavyCost = (new ShippingBox(3))->cost($rate);
  2. public function __construct(private int $weight = 1)

    pass 1 of 2
    3{4    public function __construct(private int $weight1 = 1)5    {6    }
  3. public function cost(int $rate): int

    pass 1 of 2
    8public function cost(int $rate6): int9{10    return $this->weight1 * $rate6;11}
  4. $defaultCost ← 6

    14$rate = 6;15$defaultCost→ 6 = (new ShippingBox())->cost($rate6);16$heavyCost = (new ShippingBox(3))->cost($rate6);
  5. public function __construct(private int $weight = 1)

    pass 2 of 2
    3{4    public function __construct(private int $weight3 = 1)5    {6    }
  6. public function cost(int $rate): int

    pass 2 of 2
    8public function cost(int $rate6): int9{10    return $this->weight3 * $rate6;11}
  7. $heavyCost ← 18

    15$defaultCost = (new ShippingBox())->cost($rate);16$heavyCost→ 18 = (new ShippingBox(3))->cost($rate6);1718echo "rate=" . $rate6 . "\n";19echo "defaultCost=" . $defaultCost6 . "\n";20echo "heavyCost=" . $heavyCost18 . "\n";
    outputrate=6
    defaultCost=6
    heavyCost=18