Basics of OOP in PHP Part 1

OOPS Classes and Objects
OOPS Classes and Objects

Object Oriented Programming is all about to create a modular structure for your applications. Whether your application is web based or desktop based application, OOP is a design concept with which you can create a modular code structure. Most important part of OOP concept is Classes and Objects. Let’s start getting the deeper knowledge about these fundamentals of OOP and step by step we will move towards the master level concepts of OOP.

What is an Object in Object Oriented Programming?
OOP is an approach to solve problems where all the computations are carried out with the usage of objects. Everything around you is an object, it can be anything like your cell phone, laptop, motor bike, car, and you yourself also. Every object has two things associated with it: Properties and Behaviours. Properties means the attributes /characteristics whereas Behaviours means the way to perform some actions. Let’s have a look on some examples.

Example 1:
In case of Cell Phones which we are using in our daily life.
Properties: Color, Size, Weight, Brand Name etc.
Behaviours: Calling, Messaging, Photo Capturing, Playing Music etc.

Example 2:
Similarly for Car or Motor Bike.
Properties: Color, Size, Weight, Brand Name etc.
Behaviours: Move Backward and Forward

Example 3:
This one is the most important and easiest example with which you can relate yourself easily. Just think about yourself as a Object, what would be your Properties & Behaviours? May be you dont know the answer for this, but let’s have this answer right here below:
Properties: Name, Appearance, Ethnicity, Date of Birth, Skin Tone etc.
Behaviours: Talking, Walking, Sleeping, Eating, Communicating, Learning etc.

Example 4:
Now this example will show you the objects in programming world. Let’s consider that you want to develope a small application (Area Calculator) to calculate an area of any figure, so in this case the details would be:
Object: Area Calculator
Properties: Length, Breadth, Height, Radius, etc.
Behaviours: Calculate Area by using Addition, Subtraction, Multiplication, Division, etc.

Hope, now you are clear with the basic understanding of Objects in OOP.

What is a Class?
Blueprint of your object is a Class. You can consider a Class as a concept and embodiment of this concept is the Object. To have a Class is must before you create an Object. Let’s have some examples for Classes as well to understand it better.

Example 1:
Consider your Car as an example to understand the concept behind the classes & objects. Your Car is a subset of Car Category, which means CAR is a Class and your Car is an Object (Instance) of the CAR class. Now you want to describe your car and have the Car to do something. A Class CAR would provide a blueprint for what your Car looks like & what your Car can do. To use this class CAR in your program you need to create an object for this class first. After creation of object, you can describe the car and have it perform something.

NOTE: Just give attention here that we are using CAR for class and Car for an object of CAR class.

Example 2:
Let us explain this in terms of programming. Area Calculator is the instance/object of the class Area Calculator. Area Calculator defines all the properties like length, breadth, height, and radius etc, and behaviour like area calculation by using addition, subtraction, multiplication, division. Everytime when you create an object of the class and assigning values to properties and apply area calculation behaviour to these, this will provide you the result.
Class: Area Calculator
Object/Instance: Area Calculator
Properties: Length, Breadth, Height, Radius, etc.
Behaviours: Calculate Area by using Addition, Subtraction, Multiplication, Division, etc.

Methods and Functions in OOP
Sometimes methods are confused with functions but they are not same, they are distinct. Method is a procedure associated with the class in OOP. Method defines the behaviour of the objects which are created from class or you can say that the action which the object will perform is known as method. Binding is the association between class and method. If we take Area Calculator as an example then method would be of AreaCalculation.

Function is a set of instructions which are combined to achieve an output/result. A typical function requires some input in terms of arguments and returns some output on the basis of the arguments passed. For example, consider that you want to calculate area of square or rectangle in which the formula is same but the difference is just of sides. We will create a function AreaCalculator ($length, $breadth) which will calculate the area on the basis of various different values of length and breadth. Every time you pass the arguments and call the function AreaCalculator, it will show you the results.

You must be thinking that “How does this differ from method“, Right? So here is the answer for that also.
A function not associated and independent of a class. You can use the function any where in your code, you don’t have any need to craete an object to use function. You can simply call it by passing arguments/parameters with it.
If any function is associated with the class then it becomes a method because now it’s a procedure associated with the class. Every time you create an object using class name, the method would be a part of that object.

Functionality of a method and function will be same but the change here is that the function is now bound with the class making it a method not function any more. You will not be able to use it directly, to use a method object creation would be must.

Note: In a Class, variables are called ‘properties’ and functions are called ‘methods’.

Here is the example code which you all can use to find the area of square or a rectangle. Please not that this is just the basic example program to show you about the classes, objects, and methods.

<?php
	class AreaCalculator
	{
		var $length;
		var $breadth;
		function AreaCalculation(){
			return ($this->length*$this->breadth);
		}
		
	}
	$object = new AreaCalculator();
	$object->length = 20;
	$object->breadth = 20;
	echo $object->AreaCalculation();
?>

In this code AreaCalculator is the class which is having the properties (length and breadth) of the object ($object) and AreaCalculation is the behaviour (method) of the object.

Explanation
In PHP, to create a class you need to use a keyword class and you can define your properties as a class variable & function as a class behavior. Like we have created a class AreaCalculator with the properties (length and breadth) and behaviour (AreaCalculation). AreaCalculation is a method which is bounded by a class.

To create an object in php for a class you just need to use new keyword $object = new AreaCalculator(); As you have created an object for a class, so by using it you can assign access properties and methods of the class as well.

<?php 
	$object = new AreaCalculator();
	$object->length = 20;
	$object->breadth = 20;
	echo $object->AreaCalculation();
?>

One more detailed example for the class is as given below:

<?php
	class AreaCalculator
	{
		var $length;
		var $breadth;
		function AreaCalculation(){
			return ($this->length*$this->breadth);
		}
	}

	class InterestCalculator
	{
		var $rate;
		var $time;
		var $principal;
		function InterestCalculation(){
			return ($this->rate*$this->time*$this->principal);
		}

	}

	$object = new AreaCalculator();
	$object->length = 20;
	$object->breadth = 20;
	echo "Area is: ".$object->AreaCalculation();
	echo "<br/>";

	$object_2 = new InterestCalculator();
	$object_2->rate = 25;
	$object_2->time = 2;
	$object_2->principal = 1000;
	echo "Interest is: ".$object_2->InterestCalculation();
?>

 

You may also like...

Leave a Reply