Class and Object in OOP example with PHP

Class and Object
Spread the love

To implement OOP, first, you have to knowledge about objects. In this tutorial, I am going to discuss objects and class. So, here we go.

What is Object?

An object can be anything. In this real-world anything call be an Object. If you look around the world, You will find substantive examples of an object. Like a human, table, chair, vehicle etc. All Objects have functions, attributes, behaviour, characteristics.

Let’s have an example of a Human object. A human has hands, eyes, legs etc and a human can walk can eat can play etc. So the properties of human objects are hands, eyes and legs and the behaviour of human is he/she can eat, play, work etc.

PHP objects are conceptually similar to real-world objects because they consist of state and behaviour. An object contains its state in variables that are known as properties. An object also discloses its behaviour via functions which are referred to as methods.

What is a class?

In the real world, you can find similar types of objects. For example, human has a lot of objects. We live in approximately 7.753 billion humans of this world. All humans have the same properties or same characteristics.

According to object-oriented terms, we say that an individual person is an instance of a Human class. A class is a collection of attributes and functions. A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instructions to build a specific type of object.

If you can take a copy or create an instance of a class then it is called an Object. Let’s have more examples of class and object. The vehicle can be a class. Motorcycle, Car, Truck can be the object of Vehicle Class.

Define a class

Well, now I will you how to define a class and create objects or instances in PHP. To define a class, you specify the class keyword followed by a name like this.

<?php 
class ClassName 
{ 
   // properties and methods
}

let’s create another class whose name is Human

<?php

class Human
{
}

To define a class, you have to follow these rules.

1. A class name should be in the upper camel case where each word is capitalized. For example User, ProductCategory etc

2 . Every class should be in a separate file.

3 . If a class name is a noun, it should be in the singular noun.

Well, from the Human class if you want to create an instance then you have to use new keyword.

$person1 = new Human()
$person2 = new Human()

These are called instances or objects of a class.

Add properties to a class

To add properties to the Human class, you place variables inside it. For example :

<?php
class Human {
	public $name;
	public $age;
	
}

The Human class has two properties $name and $age. In front of each property, you see the public keyword. Which is the access modified. Later I will discuss details more about access modification.

The public keyword determines the visibility of a property. In this case, you can access the property from the outside of the class.

To access a property, you use the object operator (->) like this:

$person1→name = “Shahin”;
$person1→age =  22;

Add methods to a class

Following the example, you can add a method in a class. I told you before methods are the behaviour of the class.

<?php

class ClassName {
	public function methodName (args){
		// task 
}

Like the class name, you can give the method name in camelcase. Let’s create a method in our Human class.

<?php

class Human {
	public function printName (){
	echo $this->name;
}
}

well, I define a method printName that print the property of the name. You may notice inside the method I used $this keyword to access the name property. Don’t worry in next tutorial, I will discuss more details about why I use this.

Now see how to use the method using objects. Follow this example

$person1→printName(); // Shahin

To access a method, you use the object operator (->).

Read More: Introduction with Object-oriented programming – (OOP)

Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.