Understanding Basic OOP Concepts in PHP: Part 1


Before you begin creating objects in PHP, it helps to understand some simple concepts of object -oriented programming. In subsequent sections, you explore classes, objects, properties, and methods. These include the normal building blocks that can be used to produce object-oriented programs within PHP.


In actuality, objects have attributes and behaviors. An automobile has a color, a weight, a manufacturer, and a gas tank of a certain volume. Those are some its attributes. An automobile may accelerate, stop, signal for a change, and also sound the horn. These are its behaviors. These attributes and behaviors are common to all automobiles. While unique vehicles could have unique colors, most vehicles vary in color.

Using OOP, you'll be able to model the general concept of an automobile; that is certainly, anything together with all those characteristics, by using a class. A class is often a unit of code that explains the particular attributes and behaviors of something, or associated with several grouped items. A class might be called "auto". As an example would likely explain the attributes and behaviors common to every auto.


An object is a specific instance of a class. For example, when you make an auto class, you might then go to help develop the item called "myAuto" that belongs to the auto class. You may subsequently develop a second object, yourAuto, also using the auto class.

Imagine a class like a blueprint, or factory, for constructing an object. A class describes the attributes that an object will have, but not actually the particular values of those attributes. On the other hand an object is produced while using a blueprint supplied by a class, and its attributes include certain values. For example, the auto class could possibly show basically which auto needs to have a color, whereas a specific "myAuto" object could be colored blue.

The difference between classes and objects is usually confusing to those new to OOP. It may help to consider classes seeing that anything anyone builds as you design and style the applications, although objects are created and used in the event the program will be run.


Within OOP terms, the features of any class as well as objects are usually referred to as its properties. Properties are usually just like regular variables, in that they have a name and a value. Many properties allow their value to be changed yet others will not. For example, the auto class may well have properties like color along with weight. Although the color of an auto is usually changed by giving it a new paint job, the weight of the car is a fixed value.


The particular behaviors of a class, that is, the actions of this particular class, are known as its methods. Methods are generally very similar to functions; in fact, you define methods throughout PHP using the function statement.

Like functions, some methods behave on outside data transferred to them as arguments, but an object's method may also access the properties of the object. For example, an accelerate method of the auto class may possibly examine the gasoline property to ensure it has sufficient fuel to move the auto. The method may possibly then update the object's rate property to indicate the fact that the auto has accelerated. The method of a class with its properties, are collectively known as a member of the class.

Now I am showing am example with explanation to create a class and object

To create a class, you use the "class" keyword in PHP.

class auto {
// Nothing Here

This code simply defines a class called "auto" that does nothing, it merely includes a comment. You can put code in a class within the curly brackets({ }).

Now that you have defined a class, you can create an object based on the class. For creating an object you use the "new" keyword with the name of the class that you desire to base the object on. You can then assign the object to a variable, much like any other value.

The code first defines the empty auto class as before, then we create two new instances of the "auto" class, and those objects are assigned to variables with the names $obj1 and $obj2. Basically both objects are based on the same class, but they are independent of each other and each are stored in each variable and both object's content will be displayed using the "print_r()" function and you can also see it in the following image output.


class auto


//nothing here


$obj1=new auto();

$obj2=new auto();

print_r($obj1); //display auto object

echo "</br>";

print_r($obj1); //display auto object




Similar Articles