Table of Contents
The difference between the three access control modes of public, protected and private in php, protectedprivate
The difference between the three access control modes of public, protected and private in php " >The difference between the three access control modes of public, protected and private in php
Home Backend Development PHP Tutorial The difference between PHP's three access control modes: public, protected, and private, protectedprivate_PHP tutorial

The difference between PHP's three access control modes: public, protected, and private, protectedprivate_PHP tutorial

Jul 13, 2016 am 10:03 AM
modifier

The difference between the three access control modes of public, protected and private in php, protectedprivate

The difference between the three access control modes of public, protected and private in php

public: public type

In a subclass, you can call public methods or properties through self::var, and parent::method to call parent class methods

In an instance, you can call public type methods or properties through $obj->var

protected: protected type
In a subclass, you can call protected methods or attributes through self::var, and parent::method to call parent class methods

You cannot call protected type methods or properties through $obj->var in an instance

private: Private type
The attributes or methods of this type can only be used in this class. The attributes and methods of private types cannot be called in instances of this class, subclasses, or instances of subclasses


2. The difference between self and parent
a). These two objects are commonly used in subclasses. The main difference between them is that self can call public or protected properties in the parent class, but parent cannot call

b).self:: It represents the static members (methods and properties) of the current class. Unlike $this, $this refers to the current object

Attached code:

/**
* parent can only call public or protected methods in the parent class, but cannot call attributes in the parent class
* self can call methods and attributes in the parent class except private types All data of
*/
class User{
    public $name;
    private $passwd;
    protected $email;    
    public  function __construct(){
        //print __CLASS__." ";
        $this->name= 'simple';
        $this->passwd='123456';
        $this->email = 'bjbs_270@163.com';
    }    
    public function show(){
        print "good ";
    }    
    public function inUserClassPublic() {
        print __CLASS__.'::'.__FUNCTION__." ";
    }    
    protected  function inUserClassProtected(){
        print __CLASS__.'::'.__FUNCTION__." ";
    }    
    private function inUserClassPrivate(){
        print __CLASS__.'::'.__FUNCTION__." ";
    }
}

class simpleUser extends User {    
    public function __construct(){        
        //print __CLASS__." ";
        parent::__construct();
    }
    
    public function show(){
        print $this->name."//public ";        
        print $this->passwd."//private ";
        print $this->email."//protected ";
    }
    
    public function inSimpleUserClassPublic() {
        print __CLASS__.'::'.__FUNCTION__." ";
    }
    
    protected function inSimpleUserClassProtected(){
        print __CLASS__.'::'.__FUNCTION__." ";
    }
    
    private function inSimpleUserClassPrivate() {
        print __CLASS__.'::'.__FUNCTION__." ";
    }
}

class adminUser extends simpleUser {
    protected $admin_user;
    public function __construct(){
        //print __CLASS__." ";
        parent::__construct();
    }
    
    public function inAdminUserClassPublic(){
        print __CLASS__.'::'.__FUNCTION__." ";
    }
 
protected function inAdminUserClassProtected(){
print __CLASS__.'::'.__FUNCTION__." ";
 }
 
private function inAdminUserClassPrivate(){
print __CLASS__.'::'.__FUNCTION__." ";
}
}

class administrator extends adminUser {
public function __construct(){ parent::__construct();
}
}

/**
* In an instance of a class, only public properties and methods can be called through instantiation
*/
$s = new administrator();
print '--------- ---------';
$s->show();
?>

http://www.bkjia.com/PHPjc/969249.html

truehttp: //www.bkjia.com/PHPjc/969249.htmlTechArticleThe difference between the three access control modes of public, protected and private in php, the three access control modes of public, protected and private in protectedprivate php The difference between the two access control modes public: Public types are in subclasses...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the java access control modifiers? What are the java access control modifiers? Sep 20, 2023 pm 02:43 PM

There are four types of java access control modifiers, namely public, protected, private, and default access modifiers. Detailed introduction: 1. Public, public is the loosest access control modifier. Modified classes, methods and variables can be accessed by any other class. When a class, method or variable is declared as public, they can be accessed anywhere be accessed, whether it is a class in the same package or a class in a different package; 2. protected modifier, etc.

What can be the modifiers of java interface? What can be the modifiers of java interface? Jul 03, 2023 am 10:46 AM

The modifiers of the Java interface can be: 1. public, the interface can be accessed by any code; 2. abstract, the interface itself is abstract and needs to be concretely implemented in the class that implements the interface; 3. default, you can provide a The default implementation, the implementation class can choose whether to override the method; 4. static, which can be called directly through the interface name within the interface without instantiating the interface; 5. strictfp, which can be applied between interfaces and interfaces, and between classes and interfaces. on the relationship between.

Event handlers and modifiers in Vue 3 to optimize user interaction experience Event handlers and modifiers in Vue 3 to optimize user interaction experience Sep 08, 2023 am 11:00 AM

Event handlers and modifiers in Vue3, optimizing user interaction experience Introduction: In Vue3, event handlers and modifiers are important features for optimizing user interface interaction experience. Event handlers allow us to respond to user actions and execute corresponding logic. Modifiers provide additional control and customization of event behavior. This article will introduce event handlers and modifiers in Vue3 in detail and provide some practical code examples. Event handler: In Vue3, we can bind it through the v-on directive

Detailed explanation of PHP permission control modifiers: Comprehensive understanding of commonly used permission control modifiers Detailed explanation of PHP permission control modifiers: Comprehensive understanding of commonly used permission control modifiers Jan 19, 2024 am 10:37 AM

Detailed explanation of PHP permission control modifiers: To fully understand the commonly used permission control modifiers, specific code examples are required. In PHP development, permission control is a very important concept, which can effectively ensure the security and maintainability of the code. In permission control, modifiers are essential elements. There are three modifiers in PHP: public, protected and private, which respectively represent three access rights. This article will introduce their usage and usage scenarios in detail, and provide specific

Revealing the secrets of PHP permission control modifiers: Mastering the usage skills in practical applications Revealing the secrets of PHP permission control modifiers: Mastering the usage skills in practical applications Jan 19, 2024 am 10:06 AM

PHP is a programming language widely used in web development. When developing web applications, permission control is an essential part. Permission control can ensure the data security and functional integrity of the application. PHP provides many permission control modifiers, which this article will discuss. Introduction to permission control modifiers In PHP, there are three main types of permission control modifiers: public, protected, and private. Public means public access. Its properties and methods can be accessed anywhere inside or outside the class.

Summary of PHP permission control modifiers: an overview of the uses and differences of various permission modifiers Summary of PHP permission control modifiers: an overview of the uses and differences of various permission modifiers Jan 19, 2024 am 10:28 AM

As a programming language commonly used for web development, PHP also has strict requirements for permission management. In order to ensure the security of the program, developers must control permissions on various parts of the program through permission control modifiers. This article will introduce the permission control modifiers in PHP in detail, help readers better understand their functions and differences, and provide corresponding code examples. public modifier The public modifier is the most commonly used permission control modifier in PHP and is used to describe public member variables and member methods. will a

Troubleshooting PHP permission control modifiers: Answers to frequently asked questions to help you better understand and apply permission control modifiers Troubleshooting PHP permission control modifiers: Answers to frequently asked questions to help you better understand and apply permission control modifiers Jan 19, 2024 am 09:34 AM

PHP permission control modifier troubleshooting: In web applications, permission control is a very important part, and more and more applications require user authentication and authorization to protect sensitive data. In PHP, we can use permission control modifiers to control the visibility of classes and methods for better access control. The issues this article will discuss are some common questions about PHP permission control modifiers, hoping to help readers better understand and apply permission control modifiers. What are permission control modifiers? PHP

PHP permission control modifier analysis: in-depth analysis of the characteristics and functions of various modifiers PHP permission control modifier analysis: in-depth analysis of the characteristics and functions of various modifiers Jan 19, 2024 am 10:43 AM

In web application development, permission control is a very important feature, especially when the application involves user authentication and the protection of sensitive information. In PHP, permission control modifiers are a common tool used to control access permissions to classes, properties, and methods. This article will deeply analyze the characteristics and functions of various permission control modifiers in PHP, and demonstrate their use through specific code examples. public modifier public is the most basic modifier in PHP, which means that classes, properties and methods can be accessed

See all articles