Differences between the public, protected, and private access control modes of php

Source: Internet
Author: User
FROM: blog.163.comweiwenjuan _ bjblogstatic14035033620129304183850? Suggestedreadingpublic: The public type in the subclass can call the public method or attribute through self: var, parent :: the method calls the parent class method. You can use $ obj-var in the instance to call the public type method or attribute pr.

FROM: http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850? Suggestedreading public: The public type in the subclass can call the public method or attribute through self: var, parent :: the method calls the parent class method. You can use $ obj-var in the instance to call the public type method or attribute pr.

FROM: http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850? Suggestedreading

Public: public type

In the subclass, you can use self: var to call the public method or attribute, and parent: method to call the parent method.

You can use $ obj-> var to call public methods or attributes in an instance.

Protected: protected type
In the subclass, you can use self: var to call the protected method or attribute, and parent: method to call the parent class method.

You cannot use $ obj-> var to call a protected method or attribute in an instance.

Private: private type
Attributes or methods of this type can only be used in this class. Private attributes and methods cannot be called in instances of this class, subclass, and subclass.


2. self and parentDifferences
A) These two pairs are often used in subclasses. Their mainDifferencesSelf can call public or protected attributes in the parent class, But parent cannot call

B). self: It indicates that the static member (method and attribute) of the current class is different from $ this. $ this indicates the current object.

Code:

1
 Name = 'simple'; 13 $ this-> passwd = '000000'; 14 $ this-> email = 'bjbs _ 270@163.com '; 15} 16 public function show () {17 print "good"; 18} 19 public function inUserClassPublic () {20 print _ CLASS __. '::'. _ FUNCTION __. ""; 21} 22 protected function inUserClassProtected () {23 print _ CLASS __. '::'. _ FUNCTION __. ""; 24} 25 private function inUserClassPrivate () {26 print _ CLASS __. '::'. _ FUNCTION __. ""; 27} 28} 29 30 class simpleUser extends User {31 public function _ construct () {32 // print _ CLASS __. ""; 33 parent ::__ construct (); 34} 35 36 public function show () {37 print $ this-> name. "// public"; 38 print $ this-> passwd. "// private"; 39 print $ this-> email. "// protected"; 40} 41 42 public function inSimpleUserClassPublic () {43 print _ CLASS __. '::'. _ FUNCTION __. ""; 44} 45 46 protected function inSimpleUserClassP Rotected () {47 print _ CLASS __. '::'. _ FUNCTION __. ""; 48} 49 50 private function inSimpleUserClassPrivate () {51 print _ CLASS __. '::'. _ FUNCTION __. ""; 52} 53} 54 55 class adminUser extends simpleUser {56 protected $ admin_user; 57 public function _ construct () {58 // print _ CLASS __. ""; 59 parent ::__ construct (); 60} 61 62 public function inAdminUserClassPublic () {63 print _ CLASS __. '::'. _ FUNCTION __. ""; 64} 65 66 protected function inAdminUserClassProtected () {67 print _ CLASS __. '::'. _ FUNCTION __. ""; 68} 69 70 private function inAdminUserClassPrivate () {71 print _ CLASS __. '::'. _ FUNCTION __. ""; 72} 73} 74 75 class administrator extends adminUser {76 public function _ construct () {77 parent ::__ construct (); 78} 79} 80 81/** 82 * in the class instance, only the public attributes and methods can be instantiated to call 83 */84 $ s = new administrator (); 85 print '------ ------------- '; 86 $ s-> show (); 87?>

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.