PHP Introduction | Structure and Object Orientation | What Is Object Orientation?
A structure based on functions lets you organize a long task into several smaller units. But when a program becomes larger, that alone is not enough. For example, imagine 1,000 functions lined up in the source code. Can we really call that organized?
How can we organize 1,000 methods more effectively? The first idea is to gather related things in one place and handle them together. For example, if you had 50 groups, each containing 20 related functions, the whole program would be easier to grasp. At the very least, it would be much better than having 1,000 items lined up individually.
Even if functions can be handled that way, another problem appears: what should be done with 1,000 global variables? In a long program, the number of variables used can also become enormous. If related variables can be gathered and organized in one place, the situation changes significantly.
In other words, the next step after structuring is a way to gather related features and values in one place and handle them together.
For example, create a category called “data access” and put into it various functions for reading data, searching data, and replacing parts of it. At the same time, prepare all necessary information inside it, such as the name of the data file being accessed and the structure of the data. Then set values there as needed and call the functions provided there. If you can create a category where “all data and functions related to data access are inside this,” it becomes very convenient.
You create this kind of thing for many different purposes and combine them. This makes it possible to handle programs even as they grow large.
This is the concept of an object. An object is a category that contains all the data and functions it needs inside itself and can be used anywhere as an independent program component.
The Class, the Blueprint for an Object
In PHP, an object consists of a “blueprint” and “created parts.” First, you create a blueprint that defines the contents of the object, and then you create actual working parts based on that blueprint.
This blueprint is called a class, and the parts created from a class are called instances. A class is designed in the following form.
class Class {
field1;
field2;
...... write as many as needed ......
function method1 (formal arguments) {
...... processing to perform
}
function method2 (formal arguments) {
...... processing to perform
}
...... write as many as needed ......
}
A class consists of fields and methods. A field is a variable that stores values in a class. A method is a function written inside a class that defines processing. A class is created by writing these two elements as needed.
To create an instance, which is the part you actually manipulate, from the class you created and access its fields or methods, write the following.
$variable = new Class(arguments);
$variable->field;
$variable->method(arguments);
An instance is created in the form new Class(). Depending on the class, arguments may be required at this point. This creates an instance and assigns it to a variable. To manipulate a method or field of an instance, append the -> symbol after the variable assigned to the instance, as in $variable->field or $variable->method(), and then write the field name or method name.
There are still many things that need to be explained about classes, but if you first understand how to define a class, how to create an instance from a class, and how to use the fields and methods of an instance, you will be able to start using classes right away.