Classes and Objects
Multiple Objects
The same class can create separate objects with separate values.
separate instances
Two objects made from one class can produce different scalar results.
Multiple Objects
multiple_objects.php
Replay: real traced execution (multi-file project)
<?php
class PackageBox
{
public function __construct(public int $weight)
{
}
}
$extra = 2;
$firstWeight = (new PackageBox(3))->weight;
$secondWeight = (new PackageBox(4 + $extra))->weight;
$totalWeight = $firstWeight + $secondWeight;
echo "first=" . $firstWeight . "\n";
echo "second=" . $secondWeight . "\n";
echo "total=" . $totalWeight . "\n";
<?php
class PackageBox
{
public function __construct(public int $weight)
{
}
}
$extra = 0;
$firstWeight = (new PackageBox(3))->weight;
$secondWeight = (new PackageBox(4 + $extra))->weight;
$totalWeight = $firstWeight + $secondWeight;
echo "first=" . $firstWeight . "\n";
echo "second=" . $secondWeight . "\n";
echo "total=" . $totalWeight . "\n";
<?php
class PackageBox
{
public function __construct(public int $weight)
{
}
}
$extra = 5;
$firstWeight = (new PackageBox(3))->weight;
$secondWeight = (new PackageBox(4 + $extra))->weight;
$totalWeight = $firstWeight + $secondWeight;
echo "first=" . $firstWeight . "\n";
echo "second=" . $secondWeight . "\n";
echo "total=" . $totalWeight . "\n";
$extra ← 2
9$extra→ 2 = 2; //@extra=0, 510$firstWeight = (new PackageBox(3))->weight;public function __construct(public int $weight)
pass 1 of 63{4 public function __construct(public int $weight3)5 {6 }All 6 passes — pass 1 is the card above pass $weight1 3 2 3 3 3 4 6 5 6 6 6 $firstWeight = (new PackageBox(3))->weight;
9$extra = 2; //@extra=0, 510$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$firstWeight ← 3
9$extra = 2; //@extra=0, 510$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$secondWeight = (new PackageBox(4 + $extra))->weight;
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight6;12$totalWeight = $firstWeight + $secondWeight;$secondWeight ← 6, $totalWeight ← 9
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight→ 6 = (new PackageBox(4 + $extra))->weight6;12$totalWeight→ 9 = $firstWeight3 + $secondWeight6;1314echo "first=" . $firstWeight3 . "\n";15echo "second=" . $secondWeight6 . "\n";16echo "total=" . $totalWeight9 . "\n";outputfirst=3 second=6 total=9
$extra ← 0
9$extra→ 0 = 0;10$firstWeight = (new PackageBox(3))->weight;public function __construct(public int $weight)
pass 1 of 63{4 public function __construct(public int $weight3)5 {6 }All 6 passes — pass 1 is the card above pass $weight1 3 2 3 3 3 4 4 5 4 6 4 $firstWeight = (new PackageBox(3))->weight;
9$extra = 0;10$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$firstWeight ← 3
9$extra = 0;10$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$secondWeight = (new PackageBox(4 + $extra))->weight;
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight4;12$totalWeight = $firstWeight + $secondWeight;$secondWeight ← 4, $totalWeight ← 7
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight→ 4 = (new PackageBox(4 + $extra))->weight4;12$totalWeight→ 7 = $firstWeight3 + $secondWeight4;1314echo "first=" . $firstWeight3 . "\n";15echo "second=" . $secondWeight4 . "\n";16echo "total=" . $totalWeight7 . "\n";outputfirst=3 second=4 total=7
$extra ← 5
9$extra→ 5 = 5;10$firstWeight = (new PackageBox(3))->weight;public function __construct(public int $weight)
pass 1 of 63{4 public function __construct(public int $weight3)5 {6 }All 6 passes — pass 1 is the card above pass $weight1 3 2 3 3 3 4 9 5 9 6 9 $firstWeight = (new PackageBox(3))->weight;
9$extra = 5;10$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$firstWeight ← 3
9$extra = 5;10$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;$secondWeight = (new PackageBox(4 + $extra))->weight;
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight9;12$totalWeight = $firstWeight + $secondWeight;$secondWeight ← 9, $totalWeight ← 12
10$firstWeight = (new PackageBox(3))->weight;11$secondWeight→ 9 = (new PackageBox(4 + $extra))->weight9;12$totalWeight→ 12 = $firstWeight3 + $secondWeight9;1314echo "first=" . $firstWeight3 . "\n";15echo "second=" . $secondWeight9 . "\n";16echo "total=" . $totalWeight12 . "\n";outputfirst=3 second=9 total=12