14.15 Privacy

  • Private fields and methods - elements that can only be accessed from within the class, not from the outside.
  • We need to know two things to use private elements:
    1. private’s interface is just like public’s interface.
      • List of methods (functions) and fields (everything else).
    2. You use private$ instead of self$
      • You cannot access private fields or methods outside of the class.
  • Why might you want to keep your methods and fields private?
    • You’ll want to be clear what is ok for others to access, especially if you have a complex system of classes.
    • It’s easier to refactor private fields and methods, as you know others are not relying on it.