PHP public, protected, private three kinds of access control mode difference self,parent

Source: Internet
Author: User

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

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: info-contact@alibabacloud.com 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.