PHP note: Detailed description based on object-oriented design _ PHP Tutorial

Source: Internet
Author: User
PHP note: Detailed description based on object-oriented design. Public indicates Global, which can be accessed by external sub-classes inside the class. copy the code as follows :? PhpclassTest {public $ nameJanking, $ sexmale, $ age23; function _ construct () {echo public indicates global, and can be accessed by external subclass inside the class;

The code is as follows:



Class Test {
Public $ name = 'janking ',
$ Sex = 'male ',
$ Age = 23;

Function _ construct (){
Echo $ this-> age .'
'. $ This-> name .'
'. $ This-> sex .'
';
}

Function func (){
Echo $ this-> age .'
'. $ This-> name .'
'. $ This-> sex .'
';
}
}


$ P = new Test ();
Echo'

';
$ P-> age = 100;
$ P-> name = "Rainy ";
$ P-> sex = "female ";
$ P-> func ();
?>
Public


Private indicates private, which can only be used inside the class;

The code is as follows:



Class Test {
Private $ name = 'janking ',
$ Sex = 'male ',
$ Age = 23;

Function _ construct (){
$ This-> funcOne ();
}

Function func (){
Echo $ this-> age .'
'. $ This-> name .'
'. $ This-> sex .'
';
}

Private function funcOne (){
Echo $ this-> age .'
'. $ This-> name .'
'. $ This-> sex .'
';
}
}


$ P = new Test ();
Echo'

';
$ P-> func ();
$ P-> age = 100; // Cannot access private property Test: $ age
$ P-> name = "Rainy"; // Cannot access private property Test: $ name
$ P-> sex = "female"; // Cannot access private property Test: $ female
$ P-> funcOne (); // Call to private method Test: funcOne () from context''
?>
Private


Protected indicates that it is protected and can be accessed only in the current class or subclass or parent class; magic methods related to Encapsulation:

_ Set (): The method automatically called when the property value of a private member is directly set.

_ Get (): The method automatically called when the private member attribute value is directly obtained.

_ Isset (); this method is automatically called when the isset directly checks whether the private attributes in the object are stored.

_ Unset (); is the method automatically called When unset directly deletes the private attribute of an object.

The http://www.bkjia.com/PHPjc/327164.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327164.htmlTechArticlepublic represents the global, and the class's internal external subclass can be accessed; the code is as follows :? Php class Test {public $ name = 'janking', $ sex = 'male', $ age = 23; function _ construct () {echo...

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.