The class is a blueprint or template for an object, means to create a new object we must require it’s blueprint or template.This article on Objects and Classes in PHP gives general and advance concepts of object and class implementation in PHP with an example of class and objects.
Why we write blueprint or template what is the meaning of that? See below example.
As you can see, we have two car images. The first car image represents a blueprint or template which can be used to create a new car object. The second car image represents a real car object that has been created by using a blueprint mentioned in the first car image.
I hope now the terminology blueprint or template is very much clear to you. Let’s see how to write a basic class template and understand the meaning of each element used in the creation of the class template.
1 2 3 4 | // Basic Class Template <access-modifier> class <class name> { ...class body....} |
Access Modifier:
In PHP we have several access modifiers like public, private, protected, default and much more.
class:
the class is a reserved keyword in PHP used during creation of a class template.
class name:
we can specify any valid class name except PHP reserve keywords.
class body:
Every class body contains two major elements, a first element known as property or state and a second element is known as method or behavior.
What is the meaning of property and method in the class? How do we represent a class diagram in UML notation?
Class Property:
It represents the state of an object. In below car class example, you can see we have defined color property.
Class Method:
It represents the behavior of an object. In below car class example, you can see we have defined two methods for a color property named “getColor” and “setColor”.
Class Diagram
The basic fundamental of the class is clear up to this point. Now let’s see how to create class and object in PHP Programming by example. In below example, you can see we have created a class named “car” and it contains one property color and two methods ( getter and setter) for it a color attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* Car Class*/ <?php class Car { /* Gets the color*/ return $this->color; } /* Sets the color*/ public function setColor($newColor) { $this->color = $newColor; } } /* Create a new object of car class */ $bmw = new Car(); /* Assign color property to car class object */ $bmw->setColor("Blue"); /* Retrive color property set to car class object */ //output BMW Car color: Blue |
PHP Object
An object is an instance of a class, it means that to create any new object we required class. We can create an Object of a class with the help of “new” Operator.
What is the difference between class and object?
The class is a logical entity while Object is a physical entity. So, Class does not have the lifespan, but Object does have a lifespan.
The Class represents a general concept while Object represents a specific concept. Let’s understand how?
In the above example, We have created a Car class that represents the common attributes and behaviors of all the types of cars. Now, when we create an object BMW from the Car class our primary focus is to represent specific attributes and behaviors available in BMW Cars.
Do you know more about classes and objects in PHP? Share your views and opinion in the comments below.
And don’t forget to share this post with your friends!