Share the differences among the three data types in php5

Source: Internet
Author: User
This article mainly introduces the differences between the three data types in php5. For more information, see

This article mainly introduces the differences between the three data types in php5. For more information, see

Public: public type
In the subclass, you can use self: var to call public methods or attributes. You can use parent: method to call methods in the parent class.
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. You can use parent: method to call the method in the parent class.
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. Differences between self and parent
A) These two pairs are often used in subclasses. Their main difference is that self 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:

<? Php/*** parent can only call public or protected methods in the parent class ,, attribute * self in the parent class cannot be called. All data except private methods and attributes in the parent class can be called */class User {public $ name; private $ passwd; protected $ email; public function _ construct () {// print _ CLASS __. ""; $ this-> name = 'simple'; $ this-> passwd = '000000'; $ this-> email = 'bjbs _ 270@163.com ';} public function show () {print "good";} public function inUserClassPublic () {print _ CLASS __. '::'. _ FUNCTION __. "";} pr Otected 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";} publi C 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 attributes and methods can be called Through instantiation in class instances */$ s = new administrato R (); print '-----------------'; $ s-> show ();?>

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.