Summary
And this concludes our second chapter. In this chapter, we aimed to learn as much as possible within the space available about the core OOP features of PHP. Firstly, we looked at what encapsulation means and the differences between private
, protected
, and public
. We encouraged you to use private
as your default, only relaxing this to protected
as required. public
properties are not normally encouraged as they break encapsulation.
Next, we looked at how inheritance works in PHP. We started with a very simple example and then built up to a more complex example using abstract
and final
to force or prevent inheritance as required. We also used interfaces to enforce classes implementing defined methods.
Finally, we looked at an alternative approach to building a graph of related classes that avoids using inheritance features and instead uses a technique called composition, whereby classes inherit functionality through dependency injection – the class defines what objects...