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

extra
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";
  1. $extra ← 2

    9$extra→ 2 = 2; //@extra=0, 510$firstWeight = (new PackageBox(3))->weight;
  2. public function __construct(public int $weight)

    pass 1 of 6
    3{4    public function __construct(public int $weight3)5    {6    }
    All 6 passes — pass 1 is the card above
    pass$weight
    13
    23
    33
    46
    56
    66
  3. $firstWeight = (new PackageBox(3))->weight;

    9$extra = 2; //@extra=0, 510$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  4. $firstWeight ← 3

    9$extra = 2; //@extra=0, 510$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  5. $secondWeight = (new PackageBox(4 + $extra))->weight;

    10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight6;12$totalWeight = $firstWeight + $secondWeight;
  6. $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
  1. $extra ← 0

    9$extra→ 0 = 0;10$firstWeight = (new PackageBox(3))->weight;
  2. public function __construct(public int $weight)

    pass 1 of 6
    3{4    public function __construct(public int $weight3)5    {6    }
    All 6 passes — pass 1 is the card above
    pass$weight
    13
    23
    33
    44
    54
    64
  3. $firstWeight = (new PackageBox(3))->weight;

    9$extra = 0;10$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  4. $firstWeight ← 3

    9$extra = 0;10$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  5. $secondWeight = (new PackageBox(4 + $extra))->weight;

    10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight4;12$totalWeight = $firstWeight + $secondWeight;
  6. $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
  1. $extra ← 5

    9$extra→ 5 = 5;10$firstWeight = (new PackageBox(3))->weight;
  2. public function __construct(public int $weight)

    pass 1 of 6
    3{4    public function __construct(public int $weight3)5    {6    }
    All 6 passes — pass 1 is the card above
    pass$weight
    13
    23
    33
    49
    59
    69
  3. $firstWeight = (new PackageBox(3))->weight;

    9$extra = 5;10$firstWeight = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  4. $firstWeight ← 3

    9$extra = 5;10$firstWeight→ 3 = (new PackageBox(3))->weight3;11$secondWeight = (new PackageBox(4 + $extra))->weight;
  5. $secondWeight = (new PackageBox(4 + $extra))->weight;

    10$firstWeight = (new PackageBox(3))->weight;11$secondWeight = (new PackageBox(4 + $extra))->weight9;12$totalWeight = $firstWeight + $secondWeight;
  6. $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