This is the final tutorial. You now have a basic grasp on what object oriented programming is and how you can apply it to PHP. There’s more to it such as design. If you would like more information, then go here
One question, that I’ve been avoiding during this whole series is why use object oriented programming? Why use constants, interfaces and all these complex terms in general?
The purpose of object oriented programming is to have code that is structured and organized. For the most part, you’ll find yourself in situations where you’ll be reusing code. Instead of having to recreate that same logic for each project, you can have a class that does most of the work for you.
Another reason for having such strict concepts is because you may end up sharing your class. For example, let’s say you had a database class. A lot of people connect to databases. Some people may be looking for code that can help them connect to a database easier and run certain functions. By creating your database connector as a class, you help prevent collisions in their own program because all code wrapped in a class is unique to the class only.
By defining your classes as public or private, you also prevent developers from ruining the functionality of your class. Of course they can just go into the class itself and change things around, but then they would be destroying the purpose of object oriented programming. What if your class updates? They’ll have to do it again and again. You’ll find yourself using other developer’s classes and knowing OOP will make you a better programmer.
You don’t have to use every single concept for your classes. It’s really up to you, but please utilize what you can to help make your code perform better. You don’t have to use classes for all your projects. It really depends on you and how you wish to use it. The basic rule is, if you’re doing the same thing over and over again, then you should probably create a class for it.