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 ();?>