How to inherit classes in php?

coldplay.xixi
Release: 2023-03-02 16:16:02
Original
5005 people have browsed it

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; } }
Copy after login

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();
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!