What is oops in PHP

Oops stands for object-oriented programming language. It is also known as a procedural programming language. It is a programming approach or paradigm that gives us prime consideration of the data and its associated functions. with the help of we can perform operations on data.

It allows you to structure your code more organized and modularly, making it easier to manage and maintain.

It has several advantages.

  1. It is faster and easier to execute.
  2. It provides us with the standard structure of programming language.
  3. It helps us to debug, make it easy to maintain, modify the code and work on the DRY principle ( Don’t Repeat Yourself ).
  4. It reduces the code with a shorter time.
  5. It provides higher security and less exposure to the data.
  6. The object-oriented is used to describe the application’s functionality.

What is a class in PHP?

In PHP, we can define a class by using the “class” keyword. It encapsulates the properties (attributes) and behaviours (methods).

The class is the blueprint or a set of instructions to build a specific type of object.

  1. The class is a template for objects.
  2. The class is an entity that is used to determine how an object will behave and what the object will contain.
  3. The class is used to hold the objects along with their behavior and properties.
  4. It is a programmer-defined data type, that includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

Example1

<?php
class Car {
public function name() {
    echo " This is the Simple Example of class";
}
}
$obj= new Car;
$obj->name();
?>

Output

This is a Simple Example of a class

Example 2

<?php
class Fruit {function name($parm) {
echo $parm;
  }
}
$apple = new Fruit();
echo $apple->name("Mango");
?>

Output

Mango

Example 3

<?php
class Car{
   private $name= "My Fav Car Hyundai Creta"; 
   public function display(){
      echo $this->name; 
   } 
}
$obj = new Car();
$obj->display();
?>

Output
My Fav Car Hyundai Creta

 

What is an object in PHP?

In PHP, objects can be related to the entities in real life It considers everything that we can see around us, as an object that has some attributes. An object is an instance of a class. It's created from a class blueprint using the new keyword followed by the class name, optionally passing arguments to the constructor if it's defined. Objects have properties (attributes) and methods defined by their class.

Multiple objects can be created from the same class, each having its own set of property values. Objects allow you to work with specific instances of a class, allowing for data encapsulation and code reuse.

  1. An object is an instance of class.
  2. Objects have properties (attributes) and methods defined by their class.
  3. An object is something that is used to perform a set of related activities.
  4. We can define a class once and then create one or more objects that belong to it.
  5. Multiple objects can be created from the same class, each having its own set of property values.
  6. An Object is an individual instance of the data structure defined by a class.
  7. Class methods and properties can be accessed directly through object instances.
  8. We we use the connector (Dot operator->) symbol to connect objects to their properties or methods.
Example1:
<?php  
class Office
{
// Class properties
// methods go here  
}
$obj = new Office;
var_dump($obj);
?

Example 2
<?php
class Office{
public function Chair_name(){
    echo "This is Desk Chair"."&lt;br&gt;";
}
public function price(){
   echo "RS- 500";
}
}
$obj = new Office();
$obj-&gt;chair_name();
$obj-&gt;price();
?>
Output
This is Desk Chair
RS- 500
 
Example 3
<?php  
class Room
{
  private $name= "Kitchen Room";  
  public function display(){  
      echo $this-&gt;name;  
  }  
}
$obj = new Room();
$obj-&gt;display();
? > 
Output
Kitchen Room
 

What is a Constructor in PHP?

In PHP, the constructor is used to initialize the object of properties,
In other words, we can say that it automatically calls when the object will be initializing.
If a class name and function name are similar in that case function is known as constructor.
We can create a constructor by using the "__construct" Keyword.

Example1
<?php
class Car{
  function CarModel(){
      echo "This is the CarModel function. It will automatically invoke";
  }
  function Car(){
      echo "User Defined Constructor (Class name and function Car() name is same )"."<br>";
  }
}
$obj= new Car();
$obj->CarModel();
?>
Output
User Defined Constructor (Class name and function Car() name is same )
This is the CarModel function. It will automatically invoke

Example2
<?php
class Car{
  function CarName(){
      echo "Car Name- Thar";
  }
  function __construct(){
      echo "This is predefined constructor"."<br>";
  }
}
$obj= new Car();
$obj->CarName();
?>
Output
This is the predefined constructor
Car Name- Thar

Example 3
<?php
class Car{
  public $name;
    function __construct($name){
      $this->name = $name;
  }
  function get_name(){
      return $this->name;
  }
}
$obj= new Car("THAR");
echo $obj->get_name();
?>
Output
THAR
 

What is a destructor in PHP?

In PHP, a destructor is a special type of method or function within a class.

It is automatically called when an object is destroyed or goes out of scope or the script is stopped or exited.

It is defined by using the __destruct() method name.

It will be automatically call the function at the end of the script.

It is typically used to perform cleanup tasks like closing files or database connections before an object is destroyed.


Example 1
<?php
class Car {
  public function __construct() {
      echo "Constructor called"."<br>";
  }
  public function __destruct() {
      echo "Destructor called";
  }
}
$obj = new Car(); // Output: Constructor called
unset($obj);         // Output: Destructor called
?>

Example 2
<?php
class Car {
public $name;
public $color;
function __construct($name) {
  $this->name = $name;
}
function __destruct() {
  echo "My Car name is : {$this->name}.";
}
}
$obj = new Car("Thar");
?>

Example 3
<?php  
  class car{  
  public function car(){  
     echo "User defined constructor1"."<br>";  
     }  
  }  
  class car2 extends car{  
  public function __construct(){  
     echo parent::car();  
     echo "User Defined Extended constructor2"."<br>";  
  }  
  public function __destruct(){  
    echo "Destroy";  
  }  
}
$obj= new car2();
?>  

What is an Access Modifier in PHP?

In PHP, access modifiers are used to control the visibility of class properties and methods.
Properties and methods can have access modifiers. It can be controlled where we can be accessed.
In other words, we can say that the Access Modifier allows us to alter the visibility of class members (properties and method).
There are three main access modifiers:
    1.  Public
    2.  Protected
    3.  Private

1. Public Access Modifier

   public access modifier is default, we can access the property or method from everywhere.

   In other words, we can say that it is open to use and access inside the class definition and outside the class definition.

   Public access modifiers can be accessed publicly, even from outside or inside of the scope of the class.

   Example 1

    <?php
  class Car {
      public $name;
      protected $color;
      private $Model;
  }
  $obj = new Car();
  echo $obj->name = 'THAR'; // OK
  echo $obj->color = 'RED'; // ERROR
  echo $obj->weight = 'LX Hard Top trim'; // ERROR
    ?>
    Output
   THAR
Example 2
<?php  
class Car{
public $name="RED THAR";  
  function display(){  
      echo $this->name."<br/>";  
  }
}
class Model extends Car{
function show(){  
    echo $this->name;  
   }  
}    
$obj= new Model;
echo $obj->name."<br>";  
$obj->display();
$obj->show();
?>
Output
RED THAR
RED THAR
RED THAR

2. Protected Access Modifier

   Protected access modifier is access  within the class and parent or inherited classes.
   In another words we can say the property or method can be accessed within the class and by classes derived from that class
   Example 1.
   <?php
  class Test{
      protected $a=200;
      protected $b=300;
          function add(){
              echo "Addition of (A+B): ".$sum=$this->a+$this->b."<br/>";
          }      
      }  
      class childtest extends test{
          function sub(){
              echo "Subtraction of (A-B): ".$sub=$this->a-$this->b."<br/>";
          }
      }  
      $obj= new childtest;
      $obj->add();
      $obj->sub();
    ?>  

3. Private Access Modifier

In Private access modifier the property or method can  be accessed within the class.
( it can't be access outside the class means in inherited class).

Example1
<?php
class Car{
  private $name="THAR CAR";
  private function show(){
      echo "This is private method of parent class";
  }
}
class Model extends Car{
 function show1(){
     echo $this->name;
 }
}
$obj= new Model();
$obj->show();
$obj->show1();
?>
Output Fatal error: Call to private method demo::show()

Example2
class parents{  
public $name="Thar";
protected $Model="Thar-02913";
private $price=50000;
public function show(){
  echo "Welcome : ".$this->name."<br/>";
  echo "Profile : ".$this->profile."<br/>";
  echo "Salary : ".$this->price."<br/>";
  }
}
class childs extends parents{
   public function show1(){
      echo "My New Car : ".$this->name."<br/>";
      echo "Model : ".$this->profile."<br/>";
      echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>

Example3
<?php
class parents{
public $name="Thar";
protected $model="Thar-02913";
private $price=50000;
public function show(){
  echo "Welcome : ".$this->name."<br/>";
  echo "Profile : ".$this->model."<br/>";
  echo "Salary : ".$this->price."<br/>";
  }
}
class childs extends parents{
 public function show1(){
      echo "My New Car : ".$this->name."<br/>";
      echo "Model : ".$this->model."<br/>";
      echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>
Output
My New Car : Thar
Model : Thar-02913
Price :
 

 

What is Encapsulation in PHP?

In PHP, encapsulation is a fundamental concept in object-oriented programming (oops) that binds the data and methods within a class. It provides us protective barrier around the internal workings of an object.

In PHP, encapsulation means wrapping up data members and methods (functions) in a single module. In other words, it is a technique of binding data members and methods.

Hiding the essential internal property of that module is known as data abstraction.

When we wrap up data members and methods together into a single unit that is called Encapsulation.

It allows us to change class to internal implementation without affecting the overall functioning of the system.


Advantages of Encapsulation
  1. Data Hiding
  2. Code Organization and Maintainability
  3. Code Reusability
  4. Abstraction
  5. Security and Validation
  6. Flexibility and Extensibility

Disadvantages of Encapsulation
  1. Increased Complexity
  2. Overhead in Performance
  3. Dependency on Class Structure
  4. Limited Accessibility
  5. Potential for Code Duplication
  6. Testing Challenges

Example 1
<?php  
class car{  
public $name; 
public $price; 
function __construct($n, $p)  { 
$this->name=$n; 
$this->price=$m; 

public function setPrice($pr){ 
$this->pr=$pr; 
 }  
 public function display(){  
echo  "We have Kids Toy : ".$this->name."<br/>"; 
    return $this->color=$this->pr;   
 }  
}   
// Create a Car object and set Car name, price
$obj=new car("THAR",5000000); 

// Update the car price setPrice() method
$obj->setPrice(900000); 

// Display the updated Price
echo "Price: ".$obj->display();
?>
Output
We have Kids Toy: THAR
Price : 900000

Example 2
<?php
class calculator{
var $x=100;
var $y=50;
function sum(){
      $res=$this->x+$this->y;
      echo "Addition (X+Y): ".$res."<br/>";
  }
  function sub(){
      $res=$this->x-$this->y;
      echo "Subtraction(X-Y): ".$res."<br/>";
  }
  function mult(){
      $res=$this->x*$this->y;
      echo "Multiplication (X*Y): ".$res."<br/>";
  }
  function div(){
      $res=$this->x/$this->y;
      echo "Division(x/y) : ".$res."<br/>";
  }
}
$obj= new calculator( );
$obj->sum();
$obj->sub();
$obj->mult();
$obj->div();
?>
Output
Addition (X+Y): 150
Subtraction(X-Y): 50
Multiplication (X*Y): 5000
Division(x/y) : 2
 

What is Inheritance in PHP?

In PHP, inheritance allows a class to inherit properties and methods from another class, promoting code reuse and creating a hierarchy of classes.
It means when a class derives from another class. The child class will inherit all the protected and
public properties and methods from the parent class. We can inherited class by using extends keyword.
it can have its own properties and methods.
It is a way to access one class from another class.
It is a mechanism of extending an existing class by inheriting class.

There are three main types of inheritance in PHP
  1. Single Inheritance.
  2. Multiple Inheritance (Not Supported)
  3. Multilevel Inheritance.
  4. 4.Hierarchical Inheritance

1. Single Inheritance
   In single inherit a class can inherit from only one parent class.

Example 1
<?php
class Parent {
  public $name;
  public function __construct($name) {
      $this->name = $name;
  }  
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
$dog = new Dog("Rex");
echo $dog->name . " says " . $dog->sound();
?>
Output
Rex says Woof!

Example 2
<?php
class Car {
public $name;
public $color;
public function __construct($name, $color) {
  $this->name = $name;
  $this->color = $color;
}
public function intro() {
  echo "We can travel with  {$this->name} and Its color is {$this->color}";
}
}
// Airplane is inherited from Car
class Airplane extends Car{
public function message() {
  echo "You can travel with Airplane- Boeing 747"."<br>";
}
}
$strawberry = new Airplane("Thar", "red");
$strawberry->message();
$strawberry->intro();
?>
Output
You can travel with Airplane- Boeing 747
We can travel with Thar and Its color is red

2. Multilevel Inheritance
   In multilevel inheritance, a class can inherit from a parent class, and another class can inherit from this child class, forming a chain.

Example1
<?php
 class Animal {
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
class GermanShepherd extends Dog {
  public function sound() {
      return "Woof Woof!";
  }
}
$gs = new GermanShepherd();
echo $gs->sound();
?>
 Output
Woof Woof!


Hierarchical Inheritance
Example 1
<?php
class Animal {
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
class Cat extends Animal {
  public function sound() {
      return "Meow!";
  }
}
$dog = new Dog();
$cat = new Cat();
echo $dog->sound();
echo $cat->sound();
?>
Output
Woof!
Meow!