PHP

OOP In PHP

Before starting OOP In PHP, I would suggest you learn PHP and MySQL first, you should know about them. Now let’s start with the topic.

What is OOP?

OOP stands for Object-Oriented Programming language it is a methodology of coding or in simple words, you can say it is a style of coding. We have multiple methods of coding but if we talk about PHP, there are 2 types of coding in PHP:

  1. Procedural (which we can call core PHP)
  2. Object-Oriented Programming

Object-Oriented Programming OOP is more popular than Procedural or core PHP. The first reason behind its popularity is its coding style which is more modular and reusable. If you write a code in a project using the OOP approach, so the same code can be used in other projects as well and your coding part will decrease. Let’s say you make a class now whenever you want to use that class you just have to include it, and you are done, The second reason behind its popularity is it contains a well-organized code that is easy to maintain and gets easy to debug, by debug I mean that whenever we get an error, so it gets easy to rectify them because the code is so organized. Just make sure to use OOP In PHP for making complex projects, not for normal medium projects we can use core PHP for it.

How does it work?

For understanding its work you have to understand its two approaches first. The first one is CLASS and the second is OBJECT. If you got its concept you will be able to understand 50% of OOP.

What is the class in OOP?

It’s a basic concept that is used in OOP, in class we talk about real-life entities, We can say it’s a set of instructions that will tell what properties an object will contain and how will it behave.

What is the object in OOP?

Everything is an object which we can see, in programming, it’s just an instance of OOP. Whenever we make an object we will follow the properties which are defined in the class.

Let’s discuss it deeply:

Classes and Objects

For example, you have a map of any house you want to build so, in that map there will be so many things that every single house has so that map is a class, and by following that map the house you will build will be called object. You can make multiple objects according to your desire for a single class. The class defines the structure and its objects formed by the class which means an object will contain all those things which will be defined in its class as I give you the example above all those things which will be defined in the map that house will also contain them because it’s formed by that map.

Let’s discuss another example,

Let’s say you have a car that is called a class, If you have Audi and Mercedes these are called objects.  As you know that in every car few things are common, every car has color, engine, seats, and price so all these features are present in every car, so these features are called properties. We can’t give a value of that property in an object which is not defined in the class, as I mentioned above an object will contain only those things which will be defined in the class.

Example

<?php

Class calculation{

Public  $a , $b , $c;

 Function sum(){

 $this->c  = $this->a + $this->b;

 return $this->c;

 }

 Function sub(){

 $this->c  = $this->a - $this->b;

 return $this->c;

 }

}

$c1 = new calculation();

$c1->a = 10;

$c1->b = 20;

$c2 = new calculation();

$c2->a = 40;

$c2->b = 30;

echo "value of sum is".$c1->sum()."<br>";

echo " value of sub is".$c2->sub();

?>

First, we create a class Calculation, then we declared variables or properties. We write PUBLIC which is an access modifier which means how can we use it, means it can use outside the function as well. Only declared variables will be used in the method which we created in class.

Then we create a method sum() for accessing $c variable we write $this->c which is equal to the sum of a and b, the sum of the variable “a” and variable “b” will be stored in $this->c. And then we return it. Then we create another function which is method sub() same as the sum function only values will be subtracted, now we create an object, remember for making an object you have to tell it for which function you are making the object like this, $c1=new calculation() here we create an object of class calculation, here we are assigning values to “a” and “b” $c1->a=10; $c1->b=20; we use arrow operator for accessing the property value or variable value.  We assigned values to “a” and “b” for object c1. Then we create another object c2 and assign different values to “a” and ”b”. now for printing values, We call both functions.

Output

As you can see we use the same function, but they are working differently for both objects, we can call their methods whenever we want because we have already assigned them a value.

Constructor Function

When an object gets a call so a constructor gets a call first. If we create a constructor, so PHP calls a constructor by itself when we create an object from a class.

While coding when we create variables we assign them values also but just imagine if your class has more than one variable what will you do? Are you going to assign them values one by one?it would be exhausting, to reduce extra lines of code we use constructors. Constructor functions call automatically when we create an object of any class at the same time a constructor function will call automatically. It initializes the value of properties, let’s take an example,

Example

<?php 

class person{

 Public $name;

 Public $age;

 Function __construct($na = "NO Name" , $ag = 0){

 $this->name =  $na;

 $this->age  =  $ag;

 }

 Function show(){

 echo  $this->name . " – " . $this->age;

 }

}

$p1=new person("harry","25"); //by removing these parameters you 
will see the default value in browser.

$p1-> show();
?>

Output

In the above code we create a class person here we declared two variables or only two properties name and age then we create a constructor function, and then we pass the default value of name and age in it. Remember that public $name and public $age are global variables. Now we store the value of the constructor’s variables into global variables, and then we made a function show for printing the default value of name and age. Then we made an object of a class person and then show the value of p1. Here the advantage of using the constructor function is we don’t need to assign every variable a value when the user passes a value, so it will store in $p1=new person(), if the user will not enter his name and age, so the default value will be shown.

Destructor Function

Destructor automatically called when the all work related to the object gets done.

Example

<?php

Class abc{

 Public function __construct(){

 echo "this is construct function <br>";

 }

 Public function hello(){
 
 echo "hello world <br>";

 }

 Public function __destruct(){

 echo "this is destruct function";
 }

}

$test = new abc();

$test->hello();

Firstly we create a class then we create a public function then we create a simple function and last we create destruct function. As you can see we didn’t use the object again we stopped the code now the destruct function will call by itself.

Output

Here the construct function is called first then the hello world is printed and then destruct function is called automatically.

Access Modifiers

As we know in every class there are two things that can include first is a variable or property and the second is the method we can add restrictions for accessing it which can be called access modifiers. We can use access modifiers for the restriction that who can access the variables and methods and who cannot, there 3 types of access modifiers public, protected, and private.

Public

By using the public access modifier you will be able to access the function from inside the class and you can access the function in the derived class and from outside the class.

Protected

By using the protected access modifier you will be able to access the function from inside the class and you can access the function in the derived class but not from outside the class.

Private

By using a private access modifier you will be able to access the function from inside the class but not in the derived class and outside the class.

Inheritance

Inheritance is one of the main concepts in PHP OOP. Simply it can be used to drive a class from another class to share their properties and method.

As we know that in every class there can be properties or methods so let’s say if there are two classes, class A and class B and both classes has properties or methods, now if you want to access the properties and methods of class A from class B so for this purpose PHP OOP has this feature called Inheritance. class B is called a derived class and class A is called a base class.

Example

We create a class of fruits and then we create another class of vegetables. When the vegetable class inherits the fruit class then we write a keyword extends and then we write the name of its base class “fruits”.

After the function, we made an object for fruits and create another object for vegetables. Now the $f object will access the fruit class while $v object will access the properties or method of vegetable class and fruit class as well because it inherited from the fruit class or because it’s a derived class of its base class, lets discuss the example below:

<?php

class vegetables {


  public $name;

  public $color;

  public function __construct($name, $color) {

    $this->name = $name;

    $this->color = $color; 

  }

  public function introduction() {

    echo "The vegetable is {$this->name} and the color is {$this-
>color}."; 

  }

}

// tomato is inherited from vegetables

class tomato extends vegetables {

  public function alert() {

    echo "Am I a vegetable or not? "; 

  }

}

$tomato = new tomato("tomato", "red");

$tomato->alert();

$tomato->introduction();

?>

Output

As you can see in the above code we create two classes and the tomato class is inherited from class vegetables, the object $tomato is accessing the methods of the vegetable class (base class).

Abstract Class

An abstract class is simply a layout meaning of variables and functions of a class that contains at least one abstract method.

It is a class that we use for the restriction that nobody can make the object of the class. In the abstract class, we create an abstract method but do not implement it. For implementation, we create its derived class and then we implement the method.

Example

<?php

Abstract class pC{

 Public $name;

 Abstract protected function cal($a, $b);

}

class cC extends pC{

 Public function cal($a, $b){

 echo $a + $b;

 }

}

$check = new cC();

$check->cal(20,30);

?>

In the above code, we create an abstract class PC then we declared an abstract method in the class.

Then we create a derived class of PC and we implement the method in it since we can’t create the object for an abstract class so we create an object for inherited class cC.

Note: you can change the value of parameters in the method, it is not necessary to use the same parameter value in method declaration and in method implementation.  The output is below:

Output

Polymorphism

It’s a feature in OOP In PHP, we use it when we want the same operation to behave differently in different classes. Basically in polymorphism poly means many and morph means forms when one thing has many forms it is known as polymorphism. We have two ways of implementing polymorphism first is through inheritance and the second is through the abstract class.

For example, we want to calculate the area of the 3 shapes like rectangle, circle, and octagons that are different from each other and we need to calculate the area of these shapes, in this case, we can make a function calarea() but the functionality of the function will be different as I mentioned in the definition that the function calarea() will remain same but it will behave differently in different classes.

Encapsulation

It is a feature in OOP we use for hiding the methods and variables from outside resources. Encapsulation is used to save data or information in an object from other. It means encapsulation is mainly used for protection purposes or hiding purposes.

When you want to keep your information safe, Let’s assume you create a class and you don’t want that a child class or any class access its properties so in that case, you can use encapsulation you can set access levels in Encapsulation.

 Encapsulation means wrapping up things into a single unit. We can take an example of a school bag or a handbag in which everything is secure and combined or together.

Conclusion

In this tutorial we discuss OOP in PHP we saw its types and we talk about its main pillars, firstly we discuss what is Class and Object then and their examples then we discuss Constructor and Destructor and their examples then we learn about Access Modifiers and Inheritance and we discussed what is an Abstract class and its example, in last we talk about Polymorphism and Encapsulation, hope this tutorial will help you to understand the concepts of PHP OOP if you have any query comment down below thanks.

Leave a Comment