PHP OOP
Language: PHP
<?php
class Animal
{
public function __construct(public string $name)
{
}
public function describe(): string
{
return $this->name . " makes a sound.";
}
}
class Dog extends Animal
{
public function fetch(): string
{
return $this->name . " fetches the ball.";
}
}
$d = new Dog("Rex");
echo $d->describe(); // Rex makes a sound.
echo "\n";
echo $d->fetch(); // Rex fetches the ball.
?>Output
Click Run to execute this code.