How to reference parent class method in PHP

PHPz
Release: 2023-04-19 11:09:08
Original
793 people have browsed it

PHP is a very popular scripting language that is widely used in the field of Web development, especially the development of dynamic web pages. In PHP, we often need to inherit classes to reuse code, and we also need to override the methods of the parent class to meet our needs. However, in some cases, we need to call the parent class method in the subclass. This article will introduce how to reference the parent class method in PHP.

1. Inheritance in PHP

In PHP, inheritance is one of the common code reuse technologies, which makes code writing faster and more convenient. Through inheritance, we can obtain all public methods and properties in the parent class, and we can override the methods and properties in the parent class to meet our specific needs.

We can implement class inheritance through the keyword "extends", the code is as follows:

class ParentClass { public function sayHello() { echo "Hello "; } } class ChildClass extends ParentClass { public function sayWorld() { echo "World!"; } }
Copy after login

In the above code, we define a parent class "ParentClass" and a subclass " ChildClass", and realizes the inheritance of ChildClass from ParentClass through the "extends" keyword. In ChildClass, we also define a new method "sayWorld", which will be called together with the method "sayHello" in the parent class.

2. Override the methods of the parent class

Inheriting the methods of the parent class may not necessarily meet our needs, so we can override the methods of the parent class to adapt to our specific needs. To override the methods of the parent class in a subclass, we need to use the "override" keyword.

The following is a sample code:

class ParentClass { public function sayHello() { echo "Hello "; } } class ChildClass extends ParentClass { public function sayHello() { echo "Hi "; } } $obj = new ChildClass(); $obj->sayHello();
Copy after login

In the above code, we define a parent class "ParentClass", which contains a public method "sayHello". In the subclass "ChildClass", we override the method "sayHello" in the parent class.

In this example, we create an instance $obj of ChildClass and call its method "sayHello". The result is "Hi" instead of "Hello" in the parent class.

3. Reference to methods of parent class

Sometimes, we need to call methods of parent class in subclasses to facilitate code reuse. In PHP, we can use the "parent" keyword to refer to methods in the parent class.

The following is a sample code:

class ParentClass { public function sayHello() { echo "Hello "; } } class ChildClass extends ParentClass { public function sayHello() { parent::sayHello(); //调用父类中的公有方法 echo "Hi "; } } $obj = new ChildClass(); $obj->sayHello();
Copy after login

In the above code, we define a parent class "ParentClass" and a subclass "ChildClass". In the method "sayHello" of the subclass "ChildClass", we call the method "sayHello" in the parent class through the "parent" keyword and add the string "Hi" after it.

In this example, we create an instance $obj of ChildClass and call its method "sayHello". The result is "Hello Hi", that is, the method "Hello" of the parent class and the method "Hi" of the subclass are called together.

4. Summary

Inheritance is a common code reuse technology and is also widely used in PHP. It is a common operation to override the methods of the parent class in a subclass, and referencing the methods of the parent class is an important method to achieve code reuse. This article introduces the operation of referencing parent class methods in PHP and explains it with sample code. In addition, in actual development, we also need to pay attention to the correct use and reasonable design of inheritance to avoid potential bugs and unnecessary overhead.

The above is the detailed content of How to reference parent class method in PHP. For more information, please follow other related articles on the PHP Chinese website!

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!