What are internal methods in php? How to call?

PHPz
Release: 2023-04-03 20:20:02
Original
1029 people have browsed it

With the continuous development of the PHP language, more and more developers are beginning to use PHP for web development, server-side programming and other work. In PHP, method calling is a very important part, and internal method calling is one of the things developers must understand. This article will introduce you to PHP internal method calling methods.

1. What are internal methods

In PHP, there are many methods in a class. Some of these methods can be called as submethods of other methods, and these methods are called internal methods. The benefit of internal methods is that they can reuse code, reduce code redundancy and complexity, and improve program maintainability and readability.

2. How to call internal methods

1. Use the $this keyword to call internal methods

In PHP, we can use the $this keyword to call internal methods . $this is a reference to the current object. Use $this->method() in the method to call the internal method.

For example, we have a class called Car, which has a method called run(). The code is as follows:

class Car {
    public function run(){
        echo "汽车正在行驶";
    }
}
Copy after login

We can use $this->run() in another method of the Car class to call the run() method:

class Car {
    public function run(){
        echo "汽车正在行驶";
    }
    
    public function start(){
        echo "汽车发动了!";
        $this->run();
    }
}

$car = new Car;
$car->start(); //输出:汽车发动了!汽车正在行驶
Copy after login

2. Use the parent keyword to call Internal methods in the parent class

If we override a method in the parent class in a subclass, but want to call the original method in the parent class, then we can use the parent keyword to call.

For example, in the following code, we extend the Car class to the SportsCar class and override the run() method. In the SportsCar class, use parent::run() to call the run() method in the parent class:

class SportsCar extends Car {
    public function run(){
        echo "跑车正在飞驰";
        parent::run();
    }
}

$sportsCar = new SportsCar;
$sportsCar->run(); //输出:跑车正在飞驰汽车正在行驶
Copy after login

3. Summary

In PHP, method calling is a very important part , and the use of internal methods can improve the maintainability and readability of the code. This article introduces how to call internal methods in PHP, including using the $this keyword and parent keyword to call internal methods and parent class methods. Hope this article can be helpful to PHP developers.

The above is the detailed content of What are internal methods in php? How to call?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template