PHP Introduction | Structure and Object Orientation | Why Is Object Orientation Necessary?
So far, the basics of object orientation, especially how to create and use classes, have been explained roughly. How did it feel when you read it?
“It may be useful, but it is tedious. I cannot really use it, can I?”
Many people may have thought this. It is certainly necessary when you write a large amount of code. But you may not build such huge programs, and the size of the site you create may not require classes every time. Many people probably think this way.
However, even for such people, there are several situations where not understanding object orientation becomes a problem. What does that mean?
1. Standard Features Are Being Replaced by Classes
First, and most importantly, features provided by recent PHP versions are increasingly being replaced by classes rather than functions.
For example, consider features for using XML. In the past, they were all provided as functions, but now it is becoming common to create and use an instance of a class called SimpleXMLElement.
Database access also used to provide many functions for each database, but now it is possible to use an instance of a class called PDO, or PHP Data Objects.
In this way, standard features are gradually being replaced by classes instead of functions. Considering future use, you should become familiar with object orientation.
2. Frameworks Are Mostly Classes
Recently, when a site reaches a certain scale, frameworks are often used. In particular, CakePHP is widely used as a framework that is easy to understand.
These frameworks are almost entirely made up of classes. A framework consists of a large amount of source code, and trying to build all of it with functions would result in extremely difficult code. Also, if it is not organized into classes, it becomes very complex.
To use these frameworks, knowledge of object orientation can be considered essential.
3. Classes Are Needed When Considering Maintenance
Someone who has just created a site may think, “As long as it works for now, that is fine.” However, if you want to operate the site for a long time, you must consider future maintenance.
The most important thing to remember when thinking about maintenance is that even code you wrote yourself can be forgotten after six months. As a site gradually becomes more sophisticated and the number of functions increases, it may eventually become difficult even for you to handle.
If you organize it in class form from the beginning, you can keep it reasonably tidy. Also, when enhancing features, you can respond more easily by inheriting and extending existing classes. Above all, if necessary, you can return to the original class, which gives you more room to recover when problems occur.
Considering these points, it is fair to say that knowledge of object orientation is now essential in PHP. Even if you think, “I do not need classes,” learning the basics first may prevent inconvenience when the need arises.
If you want to first learn what site construction with object orientation is like, I recommend trying the CakePHP framework. By using it, you can understand what the world of object-oriented site construction looks like.