One of the three features of object-oriented [encapsulation]

Source: Internet
Author: User

The member attributes of a class can be encapsulated, and the member methods can also be encapsulated. The so-called encapsulation is to use private keywords to modify attributes and methods. The encapsulated attributes and methods become private attributes and private methods, such private attributes and methods cannot be directly accessed and used by external objects. They can only be used inside the class, that is, they can only be accessed with $ this. Private methods are called by other internal methods, if you want external new objects to access private properties, you can declare a public method in the class. Generally, you do not need to declare it yourself, you can use the magic methods _ set and _ get. The magic methods are automatically called when needed. The magic methods both have a prefix consisting of two underscores. See the following example.

Class person {

Private $ name;

Private $ sex = "confidential ";

Function _ Get ($ proname ){

Return $ this-> $ proname;

}

}

 

$ P = new person;

Echo $ p-> sex;

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.