Explore knowledge about method access permissions in PHP classes

PHPz
Release: 2023-03-27 18:04:09
Original
568 people have browsed it

In PHP, we can use class methods to encapsulate code to achieve reuse. In class methods, we can set different access permissions (ie public, protected, private) to control the visibility and accessibility of these methods. In this article, we will focus on knowledge about method access permissions of PHP classes.

  1. public access rights

public access rights are the default access rights, which means that instances of any class can access these public methods. For example:

class MyClass {
  public function myPublicMethod() {
    // Public method code
  }
}

$obj = new MyClass();
$obj->myPublicMethod();
Copy after login

In the above code, we define a MyClass class, which has a public method myPublicMethod. We then create an instance of MyClass $obj and call the myPublicMethod method.

  1. protected access permission

protected access permission means that only the class and its subclasses can access the method. For example:

class MyClass {
  protected function myProtectedMethod() {
    // Protected method code
  }
}

class MyChildClass extends MyClass {
  public function myChildMethod() {
    $this->myProtectedMethod(); // Accessing the protected method from the child class
  }
}

$obj = new MyChildClass();
$obj->myChildMethod();
Copy after login

In the above code, we define a MyClass class and a protected method myProtectedMethod in it. We then created the MyChildClass class, which inherits from MyClass and defines a public method myChildMethod that calls the protected method myProtectedMethod in MyClass. We then create an instance of MyChildClass $obj and call the myChildMethod method.

  1. private access permission

private access permission means that only methods within the class can access the method. For example:

class MyClass {
  private function myPrivateMethod() {
    // Private method code
  }

  public function myPublicMethod() {
    $this->myPrivateMethod();
  }
}

$obj = new MyClass();
$obj->myPublicMethod();
Copy after login

In the above code, we define a MyClass class and a private method myPrivateMethod in it. Then, we define a public method myPublicMethod and call the private method myPrivateMethod in MyClass. We then create an instance of MyClass $obj and call the myPublicMethod method. At this point, we won't be able to access the myPrivateMethod method because it's private.

  1. Summary

In PHP class methods, we can use three access rights: public, protected and private to control the visibility of the method and accessibility. Public is the default access permission, which means that any object can access the method; protected means that only the class and its subclasses can access the method; private means that only methods within the class can access the method.

The above are the knowledge points related to method access permissions of PHP classes. We should choose appropriate access permissions based on the actual situation to ensure the security and maintainability of the code.

The above is the detailed content of Explore knowledge about method access permissions in PHP classes. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template