Source Code:
(back to article)
<?php // Example 1 class Car { public $make; public $model; public $year; public function __construct($make, $model, $year) { $this->make = $make; $this->model = $model; $this->year = $year; } public function getMake() { return $this->make; } public function getModel() { return $this->model; } public function getYear() { return $this->year; } } $myCar = new Car("Ford", "Mustang", 2022); echo "Make: " . $myCar->getMake() . PHP_EOL; echo "Model: " . $myCar->getModel() . PHP_EOL; echo "Year: " . $myCar->getYear() . PHP_EOL; // Output: // Make: Ford // Model: Mustang // Year: 2022 // Example 2 class Animal { public function speak() { echo "The animal speaks."; } } class Dog extends Animal { public function speak() { echo "The dog barks."; } } $myAnimal = new Animal(); $myDog = new Dog(); $myAnimal->speak(); $myDog->speak(); // Output: // The animal speaks. // The dog barks.
Result:
Report an issue