Share the difference between the three types of data in the PHP5 class, PHP5 data type
Public: Common Type
A method or property in a subclass that can invoke a public type through Self::var can call a method in the parent class through Parent::method
A method or property in an instance that can be $obj->var to invoke the public type
Protected: Protected Type
A method or property in a subclass that can call a protected type through Self::var can call a method in the parent class by Parent::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, cannot call properties in the parent class * Self to invoke all data except for methods and properties of private types in the parent class */class the user{public $name; private $PA SSWD; 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 (); The 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 ();p rint '-------------------'; $s->show (); >
http://www.bkjia.com/PHPjc/947911.html www.bkjia.com true http://www.bkjia.com/PHPjc/947911.html techarticle share the difference between the three data types in the PHP5 class, PHP5 data type Public: A method or property in a subclass that can be invoked by a self::var to invoke the type of publicly can pass the parent ...