Classes and Objects
Constructors
A constructor initializes a new object as it is created.
initialization
Constructor arguments provide the first values that an object will use.
Constructors
constructors.php
Replay: real traced execution (multi-file project)
<?php
class Ticket
{
public function __construct(public int $seat)
{
}
public function label(): string
{
return "seat " . $this->seat;
}
}
$seat = 12;
$label = (new Ticket($seat))->label();
echo "seat=" . $seat . "\n";
echo "label=" . $label . "\n";
<?php
class Ticket
{
public function __construct(public int $seat)
{
}
public function label(): string
{
return "seat " . $this->seat;
}
}
$seat = 3;
$label = (new Ticket($seat))->label();
echo "seat=" . $seat . "\n";
echo "label=" . $label . "\n";
<?php
class Ticket
{
public function __construct(public int $seat)
{
}
public function label(): string
{
return "seat " . $this->seat;
}
}
$seat = 25;
$label = (new Ticket($seat))->label();
echo "seat=" . $seat . "\n";
echo "label=" . $label . "\n";
$seat ← 12
14$seat→ 12 = 12; //@seat=3, 2515$label = (new Ticket($seat12))->label();public function __construct(public int $seat)
3{4 public function __construct(public int $seat12)5 {6 }public function label(): string
8public function label(): string9{10 return "seat " . $this->seat12;11}$label ← seat 12
14$seat = 12; //@seat=3, 2515$label→ seat 12 = (new Ticket($seat12))->label();1617echo "seat=" . $seat12 . "\n";18echo "label=" . $labelseat 12 . "\n";outputseat=12 label=seat 12
$seat ← 3
14$seat→ 3 = 3;15$label = (new Ticket($seat3))->label();public function __construct(public int $seat)
3{4 public function __construct(public int $seat3)5 {6 }public function label(): string
8public function label(): string9{10 return "seat " . $this->seat3;11}$label ← seat 3
14$seat = 3;15$label→ seat 3 = (new Ticket($seat3))->label();1617echo "seat=" . $seat3 . "\n";18echo "label=" . $labelseat 3 . "\n";outputseat=3 label=seat 3
$seat ← 25
14$seat→ 25 = 25;15$label = (new Ticket($seat25))->label();public function __construct(public int $seat)
3{4 public function __construct(public int $seat25)5 {6 }public function label(): string
8public function label(): string9{10 return "seat " . $this->seat25;11}$label ← seat 25
14$seat = 25;15$label→ seat 25 = (new Ticket($seat25))->label();1617echo "seat=" . $seat25 . "\n";18echo "label=" . $labelseat 25 . "\n";outputseat=25 label=seat 25