Home >Backend Development >PHP Tutorial >Rewriting understanding in php_PHP tutorial
We often hear the three major features of object-oriented: encapsulation, inheritance, and polymorphism, but there are many more features, so do we remember to rewrite? While studying, I simply recorded the medium rewriting method of PHP:
1) Let’s look at an example first to make it clearer
//Define parent class (can also be called base class)
class Goods {
public $goods_name = 'Goods:name';
public function sayName() {
echo $this->goods_name;
}
}
//Define a subclass (can also become an extension class)
class Book extends Goods {
public $goods_name='Book:name';
public function sayName() {
echo 'run in book';
}
}
//instantiate object
$b1 = new Book;
//Output run in book, because the subclass overrides the parent class’s sayName() method
$b1->sayName();
The next chapter summarizes the rewriting calls in php...