PHP public, protected, private three types of access control mode differences
Public: Common Type
The public method or property can be called through Self::var in a subclass, Parent::method calling the parent class method
A method or property in an instance that can be $obj->var to invoke the public type
Protected: Protected Type
The protected method or property can be called by Self::var in a subclass, Parent::method calling the parent class method
Methods or properties of the protected type cannot be called through $obj->var in an instance
Private: Proprietary type
A property or method of that type can only be used in that class, and the properties and methods of private types cannot be called in instances, subclasses, or instances of subclasses of that class
The difference between 2.self and parent
A). These two pairs of images 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
b). Self:: It indicates that static members (methods and properties) of the current class are different from $this, $this refers to the current
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
*/
Class user{
Public $name;
Private $passwd;
protected $email;
Public Function __construct () {
Print __class__. " ";
$this->name= ' simple ';
$this->passwd= ' 123456 ';
$this->email = ' [email protected] ';
}
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 ();
}
}
/**
* Only public properties and methods in an instance of a class can be instantiated to invoke
*/
$s = new Administrator ();
print '-------------------';
$s->show ();
?>
PHP public, protected, private three kinds of access control mode difference self,parent