In object-oriented programming, polymorphism allows objects to respond to the same method calls in different ways, while inheritance allows subclasses to inherit and extend parent class functions. Polymorphism is manifested in the differential response of different types of objects to the same method, such as the speak() method of animal, dog, and cat objects. Inheritance is reflected in the subclass inheriting data and methods from the parent class. For example, the employee class inherits the name and age from the human class, and adds a new salary attribute. In the actual case, the kiwi class inherits the fruit name of the fruit class, while the sports car class rewrites the getType() method in the parent class through polymorphism, realizing different responses to the same method in the car class, returning "car" and "car" respectively. Type information for "sports car".
Advanced PHP Object-Oriented Programming: Understanding Polymorphism and Inheritance
Overview
Polymorphism and inheritance are two basic concepts of object-oriented programming (OOP). Polymorphism allows objects to respond to the same method call in different ways, while inheritance allows the creation of new classes that inherit and extend the functionality of existing classes.
Polymorphism
Polymorphism allows an object to perform different operations depending on its type.
class Animal { public function speak() { echo "Animal speaks\n"; } } class Dog extends Animal { public function speak() { echo "Dog barks\n"; } } class Cat extends Animal { public function speak() { echo "Cat meows\n"; } } $dog = new Dog(); $dog->speak(); // 输出:Dog barks $cat = new Cat(); $cat->speak(); // 输出:Cat meows
Inheritance
Inheritance allows the creation of new classes (subclasses) that inherit data and methods from existing classes (parent classes).
class Person { protected $name; protected $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function getName() { return $this->name; } } class Employee extends Person { private $salary; public function __construct($name, $age, $salary) { parent::__construct($name, $age); // 调用父类构造函数 $this->salary = $salary; } public function getSalary() { return $this->salary; } } $employee = new Employee("John Doe", 30, 50000); echo "Employee name: " . $employee->getName() . "\n"; echo "Employee salary: " . $employee->getSalary() . "\n";
Practical case
Fruit class and kiwi class (inheritance)
class Fruit { protected $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } class Kiwi extends Fruit { public function __construct() { parent::__construct("Kiwi"); } } $kiwi = new Kiwi(); echo "Fruit name: " . $kiwi->getName() . "\n";
Car class and sports car class (polymorphic)
class Car { protected $make; protected $model; public function __construct($make, $model) { $this->make = $make; $this->model = $model; } public function getType() { return "Car"; } } class SportsCar extends Car { public function getType() { return "Sports Car"; } } $car = new Car("Toyota", "Camry"); $sportsCar = new SportsCar("Ferrari", "F430"); echo "Car type: " . $car->getType() . "\n"; echo "Sports car type: " . $sportsCar->getType() . "\n";
The above is the detailed content of PHP object-oriented programming advanced: understanding polymorphism and inheritance. For more information, please follow other related articles on the PHP Chinese website!