Home  >  Article  >  Backend Development  >  How to inherit classes in php?

How to inherit classes in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-17 11:27:035079browse

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.

How to inherit classes in php?

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;
    }
}

How to inherit classes in php?

2. Instantiate the Fruit you just created. Since there is a constructor in this class, the function will be called immediately after instantiation

How to inherit classes in php?

3. Save the above content and view the print result in the browser

How to inherit classes in php?

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();

How to inherit classes in php?

5. Save the above content and view the print of the subclass in the browser

How to inherit classes in php?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn