Differences between public, private, and protected attributes in php5

Source: Internet
Author: User
Public: The public attribute or method can be called in the subclass through self: var or self: method. you can call the method in the parent class through parent: method, however, the public owner cannot be called.

Public: public property or method

You can use self: var or self: method in a subclass to call methods in the parent class, but not public attributes.

You can use $ obj-> var or self: method to call an instance.

Protected: protected type

You can use self: var or self: method in the subclass to call methods in the parent class. you can use parent: method to call methods 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.

Difference between self and parent

A) these two objects 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, and $ this indicates the current object.

The instance code is as follows:

  1. Class BaseClass {
  2. Public $ public = 'public ';
  3. Private $ private = 'private ';
  4. Protected $ protected = 'protected ';
  5. Function _ construct (){
  6. }
  7. Function print_var (){
  8. Print $ this-> public; echo' ';
  9. Print $ this-> private; echo' ';
  10. Print $ this-> protected; echo' ';
  11. }
  12. }
  13.  
  14. Class Subclass extends BaseClass {
  15. // Public $ public = 'public2 ';
  16. Protected $ protected = 'protected2 ';
  17. Function _ construct (){
  18. Echo $ this-> protected; // accessible, because the class is defined as protected, so this class or subclass can be used, and the value can be paid repeatedly in the subclass.
  19. Echo' ';
  20. Echo $ this-> private; // error because it is private, it can only be used in the baseclass class defined by her.
  21. }
  22. }
  23. $ Obj1 = new BaseClass ();
  24. $ Obj1-> print_var ();
  25. // Echo $ obj1-> protected; // error is protected and can be called only within the class or in the parent class of the subclass.
  26. // Echo $ obj1-> private; // The error is the same as the private one. it is called only in this class.
  27. Echo $ obj1-> public;
  28. Echo" ";
  29. $ Obj2 = new Subclass ();
  30. Echo' ';
  31. Echo $ obj2-> public; echo' ';
  32. Echo $ obj2-> protected;
  33. // Echo $ obj2-> private; // error
  34. // Echo $ obj2-> protected; // error
  35. ?>

Summary:

Public indicates global and can be accessed by external subclass inside the class;

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

Protected indicates that it is protected and can be accessed only in this class or subclass or parent class;

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.