Explanation of PHP class Encapsulation

Source: Internet
Author: User

Public: methods or attributes can be accessed in any scope, and this is the default value. If an access modifier is not specified for an attribute or method, It will be public.

Protected: A method or attribute can only be accessed from a class or a member of an inherited class.

Private: methods or attributes can only be accessed from one member of the class, and cannot be accessed from members of the inherited class. Methods or attributes marked by private can be redefined in the inheritance class. Each class can only see its own private method.

The three modifiers should be sorted from large to small in terms of scope:

Public → protected → private is in the scope of action because some attributes and methods are encapsulated by PHP classes like our previous functions. This encapsulation determines the "visibility" of the data, so that we can only call the defined attributes and methods without modifying them outside the class. This is the benefit of encapsulation, it also improves security. To illustrate the functions of these modifiers, see the following code:

 
 
  1. <? Php
  2. Class myClass {
  3. Public $ public = "Public ";
  4. Protected $ protected = "Protected ";
  5. Private $ private = "Private ";
  6. Function say_Hello (){
  7. // I will not do anything now. I will tell you later
  8. }
  9. }
  10. $ Obj = new myClass ();
  11. Echo $ obj-> public;
  12. // Echo $ obj-> protected;
  13. // Echo $ obj-> private;
  14. ?>
  15. <? Php
  16. Class myClass {
  17. Public $ public = "Public ";
  18. Protected $ protected = "Protected ";
  19. Private $ private = "Private ";
  20. Function say_Hello (){
  21. // I will not do anything now. I will tell you later
  22. }
  23. }
  24. $ Obj = new myClass ();
  25. Echo $ obj-> public;
  26. // Echo $ obj-> protected;
  27. // Echo $ obj-> private;
  28. ?>

Run the above example to get a "Public", but when you remove the comments of // echo $ obj-> private;, you will get the following error: Fatal error: cannot access protected property myClass: $ protected in E: apachehtdocsexamplefile. php on line 13.

We can see that we cannot define the attributes of the runtime class at will, that is, we cannot modify some operations already defined in the PHP class encapsulation, Which Is visibility. We do not know the Members in this class in the "outside", because these Members may be invisible to other classes.

Of course, if we must access or modify the attribute defined as "private", we can also use the system methods provided by PHP: _ get () and _ set (). I will learn more about this in the future, because I only know about it now.
 


Related Article

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.