PHP public, protected, private three types of access control mode differences
Public: Common Type
在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法在实例中可以能过$obj->var 来调用 public类型的方法或属性
Protected: Protected Type
在子类中可以通过self::var调用protected方法或属性,parent::method调用父类方法在实例中不能通过$obj->var 来调用 protected类型的方法或属性
Private: Proprietary type
该类型的属性或方法只能在该类中使用,在该类的实例、子类中、子类的实例中都不能调用私有类型的属性和方法
The difference between self and parent
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
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->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: http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading
If you want to reprint please specify the Source: http://www.cnblogs.com/zhuchenglin/p/8408654.html
Public, protected, private three access control modes in PHP and the difference between self and parent (RPM)