Back to Top

Objects and Classes in PHP

Objects and Classes in PHP

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.

Objects and Classes example in PHP

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.

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.

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.

Object creation in PHP

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!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

WordPress Template Hierarchy Tutorial

Posted on 8 years ago

Bhumi

The Readers’ Poll – June 2013

Posted on 10 years ago

Bhumi

The Readers’ Poll – July 2012

Posted on 11 years ago

Bhumi