PHP OOP
Language: PHP
<?php
class Car
{
public string $color;
public int $speed = 0;
public function accelerate(int $amount): void
{
$this->speed += $amount;
}
}
$first = new Car();
$first->color = "red";
$first->accelerate(30);
$second = new Car();
$second->color = "blue";
echo $first->color . " car at " . $first->speed; // red car at 30
echo "\n";
echo $second->color . " car at " . $second->speed; // blue car at 0
?>Output
Click Run to execute this code.