Home > Backend Development > PHP Problem > Let's talk about PHP parent class private methods

Let's talk about PHP parent class private methods

PHPz
Release: 2023-04-03 17:52:01
Original
714 people have browsed it

PHP is a very popular server-side scripting language that is often used to develop web applications. In PHP, we can define a class, which can have properties and methods. The access modifiers of class methods include public, private, and protected. Among them, public means that this method can be accessed outside the class, while protected and private cannot.

In an inheritance relationship, a subclass can inherit the public methods and properties of the parent class, but the subclass cannot inherit the private methods of the parent class. This article will discuss related issues about private methods of PHP parent classes.

First, we need to understand private methods.

Private methods refer to methods that can only be accessed within the class and cannot be accessed outside the class or subclasses. In PHP, we can define private methods by adding private in front of the method. For example:

class ParentClass {
    private function privateMethod() {
        // some code here
    }
}
Copy after login

In this example, privateMethod() is a private method that can only be accessed in the ParentClass class. If we try to access this method outside the class or in a subclass, an error will be reported.

When we define a subclass, the subclass will inherit the public and protected methods and properties of the parent class. But for private methods in the parent class, subclasses cannot inherit them. This is because inheritance is an is-a relationship, while private methods belong to a has-a relationship.

The following is an example:

class ParentClass {
    private function privateMethod() {
        echo "This is a private method.";
    }
    
    protected function protectedMethod() {
        echo "This is a protected method.";
    }
}

class ChildClass extends ParentClass {
    // some code here
}

$childObj = new ChildClass();
$childObj->protectedMethod(); // This is a protected method.
$childObj->privateMethod(); // Fatal error: Call to private method ParentClass::privateMethod() from context...
Copy after login

In this example, we define a ParentClass class that contains a private method privateMethod() and a protected method protectedMethod(). We also define a ChildClass class, which inherits the ParentClass class.

When we call the protected method protectedMethod() in the child class, it works fine, but when we try to call the private method privateMethod() of the parent class, a fatal error occurs.

So, how to access the private methods of the parent class in the subclass? One way is to use the parent class's public or protected method to call the private method.

For example:

class ParentClass {
    private function privateMethod() {
        echo "This is a private method.";
    }
    
    protected function protectedMethod() {
        echo "This is a protected method.";
        $this->privateMethod(); // call private method
    }
}

class ChildClass extends ParentClass {
    public function publicMethod() {
        $this->protectedMethod(); // call protected method
    }
}

$childObj = new ChildClass();
$childObj->publicMethod(); // This is a protected method. This is a private method.
Copy after login

In this example, we call the private method in the protected method of the parent class, and define a public method publicMethod() in the subclass, which calls The protected method protectedMethod(). When we call the publicMethod() method of the subclass, it will output "This is a protected method. This is a private method."

Summary

In PHP, private Methods are methods that can only be accessed within a class. A child class cannot inherit the private methods of the parent class, but it can access them by including a public or protected method in the parent class. This means that you need to pay attention to access to the private methods of the parent class when inheriting to avoid unexpected errors.

The above is the detailed content of Let's talk about PHP parent class private methods. 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