Module Development

Make PHP Class Properties Private for Drupal 8, 9

Sprout Video

In this PHP tutorial, 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.

Additional resources

Visibility (PHP Manual)