Public, protected, private three access control modes in PHP and the difference between self and parent (RPM)

Source: Internet
Author: User

PHP public, protected, private three types of access control mode differences
    1. Public: Common Type

      在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法在实例中可以能过$obj->var 来调用 public类型的方法或属性
    2. Protected: Protected Type

      在子类中可以通过self::var调用protected方法或属性,parent::method调用父类方法在实例中不能通过$obj->var 来调用  protected类型的方法或属性
    3. Private: Proprietary type

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

    2. Self:: It represents the static members (methods and properties) of the current class and \ (this differs, \)this refers to the current object

Attached code:

<?php/*** Parent can only call public or protected methods in the parent class and cannot invoke properties in the parent class* Self can call all data except methods and properties of private types in the parent class */classuser{ Public $name;    Private $passwd;    protected $email;         Public  function __construct(){//print __class__. " ";        $this->name=' Simple ';        $this->passwd=' 123456 ';        $this->email =' [email protected] ';} Public functionShow(){Print "Good";} Public functionInuserclasspublic(){Print __class__.'::'.__function__." ";}protected  functioninuserclassprotected(){Print __class__.'::'.__function__." ";}Private functionInuserclassprivate(){Print __class__.'::'.__function__." ";}}classSimpleuserextendsUser { Public function __construct(){//print __class__. " ";        Parent::__construct();} Public functionShow(){Print $this->name."//public";                Print $this-&GT;PASSWD."//private";        Print $this->email."//protected";} Public functionInsimpleuserclasspublic(){Print __class__.'::'.__function__." ";}protected functioninsimpleuserclassprotected(){Print __class__.'::'.__function__." ";}Private functionInsimpleuserclassprivate(){Print __class__.'::'.__function__." ";}}classAdminUserextendsSimpleuser {protected $admin _user;     Public function __construct(){//print __class__. " ";        Parent::__construct();} Public functionInadminuserclasspublic(){Print __class__.'::'.__function__." ";}protected functioninadminuserclassprotected(){Print __class__.'::'.__function__." ";}Private functionInadminuserclassprivate(){Print __class__.'::'.__function__." ";}}classAdministratorextendsAdminUser { Public function __construct(){Parent::__construct();}}/*** Only public properties and methods in an instance of a class can be instantiated to invoke */$s=NewAdministrator();Print '-------------------';$s->show();?>

This article refers to address:
If you want to reprint please specify the Source:

Public, protected, private three access control modes in PHP and the difference between self and parent (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.