Visibility and Constructors
Constructor Defaults
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
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";
$rate ← 4
14$rate→ 4 = 4; //@rate=2, 615$defaultCost = (new ShippingBox())->cost($rate4);16$heavyCost = (new ShippingBox(3))->cost($rate);public function __construct(private int $weight = 1)
pass 1 of 23{4 public function __construct(private int $weight1 = 1)5 {6 }public function cost(int $rate): int
pass 1 of 28public function cost(int $rate4): int9{10 return $this->weight1 * $rate4;11}$defaultCost ← 4
14$rate = 4; //@rate=2, 615$defaultCost→ 4 = (new ShippingBox())->cost($rate4);16$heavyCost = (new ShippingBox(3))->cost($rate4);public function __construct(private int $weight = 1)
pass 2 of 23{4 public function __construct(private int $weight3 = 1)5 {6 }public function cost(int $rate): int
pass 2 of 28public function cost(int $rate4): int9{10 return $this->weight3 * $rate4;11}$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
$rate ← 2
14$rate→ 2 = 2;15$defaultCost = (new ShippingBox())->cost($rate2);16$heavyCost = (new ShippingBox(3))->cost($rate);public function __construct(private int $weight = 1)
pass 1 of 23{4 public function __construct(private int $weight1 = 1)5 {6 }public function cost(int $rate): int
pass 1 of 28public function cost(int $rate2): int9{10 return $this->weight1 * $rate2;11}$defaultCost ← 2
14$rate = 2;15$defaultCost→ 2 = (new ShippingBox())->cost($rate2);16$heavyCost = (new ShippingBox(3))->cost($rate2);public function __construct(private int $weight = 1)
pass 2 of 23{4 public function __construct(private int $weight3 = 1)5 {6 }public function cost(int $rate): int
pass 2 of 28public function cost(int $rate2): int9{10 return $this->weight3 * $rate2;11}$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
$rate ← 6
14$rate→ 6 = 6;15$defaultCost = (new ShippingBox())->cost($rate6);16$heavyCost = (new ShippingBox(3))->cost($rate);public function __construct(private int $weight = 1)
pass 1 of 23{4 public function __construct(private int $weight1 = 1)5 {6 }public function cost(int $rate): int
pass 1 of 28public function cost(int $rate6): int9{10 return $this->weight1 * $rate6;11}$defaultCost ← 6
14$rate = 6;15$defaultCost→ 6 = (new ShippingBox())->cost($rate6);16$heavyCost = (new ShippingBox(3))->cost($rate6);public function __construct(private int $weight = 1)
pass 2 of 23{4 public function __construct(private int $weight3 = 1)5 {6 }public function cost(int $rate): int
pass 2 of 28public function cost(int $rate6): int9{10 return $this->weight3 * $rate6;11}$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