Classes and Objects
Multiple Objects
The same class can create separate objects with separate values.
Multiple Objects
multiple_objects.php
<?php
class PackageBox
{
public function __construct(public int $weight)
{
}
}
$extra = ;
$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 = ;
$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 = ;
$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";
separate instances
Two objects made from one class can produce different scalar results.