Home > Article > Backend Development > How to inherit classes in php?
php method of inheriting classes: first create a PHP document and define a class; then instantiate the created Fruit; then create a blank subclass Apple and instantiate it; finally save the above content and Just use the browser to view the print of the subcategory.
php inheritance class method:
1. Create a PHP document and define a class, example:
class Fruit { public $name = 'apple'; public function __construct($name=''){ echo '我是一个' . $this->name; } }
2. Instantiate the Fruit you just created. Since there is a constructor in this class, the function will be called immediately after instantiation
3. Save the above content and view the print result in the browser
4. Create a blank subclass Apple and instantiate it , (the extends keyword must be used for inherited classes) Example:
class Apple extends Fruit { } new Apple();
5. Save the above content and view the print of the subclass in the browser
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to inherit classes in php?. For more information, please follow other related articles on the PHP Chinese website!