Make PHP Class Properties Private

Video loading...

Transcript language code: 

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:03
    with Leanna Pelham
  • 0:05
    Let me show you a problem with our app.
  • 0:08
    There's nothing stopping me from going in and setting the strength
  • 0:12
    to something like Jar Jar Binks.
  • 0:15
    Clearly, this value makes absolutely no sense at all for many reasons.
  • 0:21
    Sure enough, when we refresh, Jar Jar Binks
  • 0:23
    prints out as the strength in the Select menu.
  • 0:28
    If we tried to battle, this would probably break our app
  • 0:31
    since you can't compare a strength of 10
  • 0:33
    to Jar Jar Binks mathematically.

Make PHP Class Properties Private


In this lesson, you'll learn about to change the visibility of properties to private and how this affects how these properties can be accessed. You'll change the public properties to private and then add "getter" and "setter" methods to the class to enable controlled access of the values of these private properties.

Note: the word "hooks" in this video does not refer to hooks in Drupal's API.

Note: PHP does provide magic methods for getting and setting which are explained in this tutorial: Magic Methods: __toString(), __get, __set.

Log in or sign up to download companion files.
Additional resources: