A detailed explanation of PHP class Encapsulation _php Tutorial

Source: Internet
Author: User

Public: The method or property can be accessed at any scope, and this is the default, and it will be public if no access modifier is specified for a property or method.

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

Private: The method or property can only be accessed from one member of the class and cannot be accessed from members of the inheriting class. A method or property that is marked by private can be redefined in the inheriting class. Each class can see only the private methods it defines itself.

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

Public→protected→private is scoped because the class, like our previous functions, encapsulates some properties and methods in the PHP class. This encapsulation determines the "visibility" of the data so that we cannot arbitrarily modify the defined properties and methods outside of the class to make calls only, which is the benefit of encapsulation and also improves security. To illustrate the effects 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'm not doing anything now, I'll 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'm not doing anything now, I'll tell you later.
  22. }
  23. }
  24. $ obj = New MyClass ();
  25. Echo $obj- > Public ;
  26. //echo $obj- > protected;
  27. //echo $obj- > private;
  28. ?>

By running the example above we get a "public", but when you remove//echo $obj->private; comments, you get the following error: Fatal error:cannot Access protected property MyClass:: $protected in e:apachehtdocsexamplefile.php on line 13.

It can be seen that we are not at liberty to access the class's attribute definitions, that is, we cannot modify some of the actions already defined in the package of the PHP class, which is visibility. We do not know in the "outside" that there are members in this class, because these members may not be visible to other classes.

Of course, if we have to access or modify properties defined as "private," you can also use the system methods provided by PHP: _get () and _set (). About this in the future to understand, because now I just have a understanding of it.


http://www.bkjia.com/PHPjc/446066.html www.bkjia.com true http://www.bkjia.com/PHPjc/446066.html techarticle Public : The method or property can be accessed at any scope, and this is the default, and it will be public if no access modifier is specified for a property or method. Protected: Fang ...

  • 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.