When applications start growing, representing more complex data structures becomes necessary. Primitive types like integers, strings, or arrays are not enough when you want to associate specific behavior to data.
Object oriented programming (OOP) is an approach to programming that uses classes and objects. In object-oriented programming, the elements of a script are objects. The objects represent the elements of the problem your script is meant to solve. For example, if the script is related to a used-car lot, the objects are probably cars and customers. Or if the script is related to outer space, the objects would probably be stars and planets.
Classes and Objects
The basic elements of object-oriented programs are objects. Objects are representations of real-life elements. Each object has a set of attributes that differentiates it from the rest of the objects of the same class, and is capable of a set of actions.
For example, a car is an object. A car has properties (also called attributes), such as color, model, engine, and tires. A car has things it can do such as move forward, move backward, park, etc.In general, objects are nouns. Like nouns, objects often are more conceptual. For example, a bank account isn’t something you can hold in your hand, but it can be considered an object.
A class is the PHP code that serves as the template, or the pattern, that is used to create an object. The class defines the properties, the attributes, of the object. It also defines the things the object can do - its responsibilities. For example, you write a class that defines a car as four wheels and an engine, and the class lists the things a car can do, such as move forward and park.
An object is a complex, user-defined data type. The process of creating an object from a class is called instantiation. An object is an instance of a class.
Objects have properties, also called attributes. A car may be red, or green - a color property. Properties such as color, size, or model for a car, are stored inside the object. Properties are set up in the class as variables.
The variables that store properties can have default values, can be given values when the object is created, or values can be added or modified later.
The things objects can do are referred to as responsibilities. For example, a Car object can move forward, stop, back up, and park. Each thing an object can do, each responsibility, is programmed into the class and called a method.
In PHP, methods use the same syntax as functions. Although the code looks like the code for a function, the distinction is that methods are inside a class. It can’t be called independently of an object.
Objects should contain only the properties and methods they need.
Child classes inherit all the properties and methods from the parent class. But they can also have their own individual properties.
A child class can contain a method with the same name as a method in a parent class. In that case, the method in the child class takes precedence for a child object.
Developing an Object-Oriented Script
Object-oriented scripts require a lot of planning. You need to plan your objects and their properties and what they can do. Developing object-oriented scripts includes the following procedures:
- Choose the objects.
- Choose the properties and methods for each object.
- Create the object and put it to work.
Creating and Using an Object
After you decide on the design of an object, you can create and then use the object. The steps for creating and using an object are as follows:
1. Write the class statement.
The class statement is a PHP statement that is the blueprint for the object. The class statement has a statement block that contains PHP code for all the properties and methods that the object has.
2. Include the class in the script where you want to use the object.
You can write the class statement in the script itself. However, it is more common to save the class statement in a separate file and use an include statement to include the class at the beginning of the script that needs to use the object.
3. Create an object in the script.
You use a PHP statement to create an object based on the class. This is called instantiation.
4. Use the new object.
After you create a new object, you can use it to perform actions. You can use any method that is inside the class statement block.