Entry_03 // Date: 2026-03-25 17:15:35

Interview Prep : Object Oriented Programming (with PHP)

Are you prepping for an interview and want a quick review of PHP OOP?. This is for You !. This tutorial is under the assumption that you have a solid understanding of php basics, functions, arrays and file structures.

What is OOP?

OOP( Object Oriented Programming)  is a programming paradigm based on organizing software design around data or objects rather than functions.
It uses classes as blueprint to create objects that contain both data and their behaviours.

 

Simply put its ideal for building scalable reusable and maintainable code.
 
In comparision to procedural programming, its faster, better structured, less code and development time.
 
Two main aspects of object oriented programming are Classes and Objects.

Classes are a template for objects, and objects are individual instances of a class.

Classes are Fruits and Objects are Apple, Oranges of the Fruits.

Lets Talk More about Classes

A Class is a template for objects and defines the structure of the properties and methods of an object.It is defined with the class keyword followed by the name of the class with a pair of curly braces.
 
In this example below we are going to make a class for cars.

<?php
class Car{
    //Properties 
    public $model;
    public $color; 

    // method 
    function set($model, $color){
        // The 'this' keyword is used within a method to refer to the current object's properties and other methods.
        $this->model = $model;
        $this->color = $color;
    }
      // Method to display the properties
    function get() {
        echo "Model: " . $this->model . ". Color: " . $this->color .".<br>";
    }
}

?>

Now Objects!

Objects are the instance of a class. We can create multiple objects within a class & each Object will inherit all properties and methods of that class

Objects of a class are created with the 'new' Keyword.

Lets create two objects for our class Car.

// First Object - an object named tesla 

$tesla = new Car(); // Tesla inherits all the methods and props of the class Car
$tesla->set("Model 3", "Red"); // Set Properties of Tesla 
$tesla->get(); // 

<!-- Second Object  -->
$jaguar = new Car(); // Jaguar inherits all the methods and props of the class Car
$jaguar->set("F-pace", "gray"); 
$jaguar->get();

Constructor

When we create a new Class we want certain action to perform automatically without having to call the function.
This prevent the need to continually repeat ourselves anytime a new object is created.
 
Looking at our previous example we can make sure that When an object is created. The properties are automatically set so we dont repeat ourselves

<?php 
class Car{
    //Properties 
    public $model;
    public $color; 

    // method 
    function _construct($model, $color){
        // The this keyword is used within a method to refer to the current object's properties and other methods.
        $this->model = $model;
        $this->color = $color;
    }
      // Method to display the properties
    function get() {
        echo "Model: " . $this->model . ". Color: " . $this->color .".<br>";
    }
}


$tesla = new Car();
$tesla->get(); 


$jaguar = new Car();
$jaguar->get();
?>

Destructors

we can also do the same if we want an action to be called when an object is destroyed or when the script has completed running.

the __destruct function serves as the opposite of the __construct.

<?php

class Car{
    //Properties 
    public $model;
    public $color; 

    // method 
    function _construct($model, $color){
        // The this keyword is used within a method to refer to the current object's properties and other methods.
        $this->model = $model;
        $this->color = $color;
    }
      // Method to display the properties
    function _destruct() {
        echo "Model: " . $this->model . ". Color: " . $this->color .".<br>";
    }
}

$tesla = new Car();
$jaguar = new Car();

?>

Accessing Our Methods and Properties : Access Modifiers

the main aim of an access modifier is to restrict or allow what properties or methods can be accessed and where they can be accessed.

public - the property or method can be accessed anywhere.
protected- can be acccessed only wihin the class (or derived classes - we will discuss later).
private - the property or method can be accessed only within the same class.
 
 
First Lets set our color of car to public & create a new method in our class Car.

<?php 
public $color;

public function get_car_color(){
    echo  "Color: " . $this->color . ".";
}

$tesla->color = "Pink"; // Color of tesla accessed and changed directly 


// Even without using the public key word, we still have access this is because methods and properties are public by
default

//Lets create a new property and make it protected 


protected $weight; 

public function setWeight($weight) {
    $this->weight = $weight;
  }


$tesla->weight = "80Kg"; // Error, cant access protected property 


// Lets See how it can be accessed in a derived class - i ll dive more on this later

```
class Suv extends Car {
    public function getWeight(){
        echo "Weight: ". $this->weight. " .";
    }   
}

$ford->setWeight("150Kg")
$ford->getWeight(); // 
?>

Inheritance 👑

Let go back to our suv example that make have left you confused🤷. We want to create a new class Suv that has
a name, color and weight like the car and all the method. Basically We are trying to inherit the properties and methods of the Car Class to our Suv.
 
Inheritance in PHP OOP allows a class to inherit all the properties and methods of another class. The class that want to inherit is called the child class(Suv in our example) while the class being inherited from is the Parent (Car).

<?php
// Parent Class 
class Car{
    public $model;
    public $color;
    protected $weight; 

    public function _construct($model, $color){
        $this->model = $model;
        $this->color = $color; 
    }

    public function _destruct() {
        echo "Model: " . $this->model . ". Color: " . $this->color .".<br>";
    }

    protected function getModelType(){
        echo "Model: " . $this->model .
    }
}


// Derived Class 

class Suv extends Car{

    public function getWeight(){
        echo "Weight: ". $this->weight. " .";
    } 

    public function message(){
        echo "Welcome to BackOnDev Motors";
    }

    public function get_model(){
        $this->getModelType();
    }
}
?>

Overriding methods

So what happens if the derived class has a method named the same name as the parent class, well something happens called overridding. The method ends up replacing the parent class method.

We can prevent our class from being inherited from or overrridden by using the keyword final.
 
 

<?php
final class Car{
    // code 
}



class Car{
    final public function intro(){
        // Code 
    }
}
?>

Abstract Class & Abstract Methods

An abstract class is a "blueprint" class that cannot create its own objects, used only to be inherited by other classes, while an abstract method is a method declared without a body (no implementation) that must be implemented by child classes.

<?php 
abstract class Car{
    public $model;

     public function __construct($model) {
    $this->model = $model;
  }

// Abstract method - forces child classes to implement it
  abstract public function intro();
}


class Suv extends Car{
    public function intro() {
    return "Italian Cars are the best";
  }
}

// The intro class must be implemented in all the child classes. This applies also to the number of required arguments, 
// the same or less access modifiers also. 
?>

PHP Interfaces

An interface lets you define a method without restrictions as to how it is implemented.
 
Interfaces can't have properties while abstract methods can.
Interfaces must be public. while abstracts can be public or protected.
When one or more classes use the same interface, it is referred to a polymorphism.

<?php 
// Lets test with a new example : 

interface Car{
    public function makeSound();
}

class Suv implements Car{
    public function makeSound(){
        echo "Zoom!... ";
    }
}

class Bus implements Car{
    public function makeSound(){
        echo "Brr...";
    }
}


$tesla = new Suv();
$tesla->makeSound();

$school_bus = new Bus();
$school_bus->makeSound();
?>

Traits

Traits is mechanism for code reuse. They are used to declare methods that can be used in multiple classes.
Traits can have method and abstract methods and the methods can have any of the access modifiers.

<?php
trait logger{
    public function log(){
        echo "Hey My Name is Richey, And I am your car dealer";
    }

    public function message($message){
        echo "Hey My Name is Richey, And I am your car dealer";
    }
}

class Car{
    use logger;

     public function createUser() {
        $this->log();
    }

     public function createUserMessage() {
        $this->message("Welcome To Motor Inc.");
    }
}
?>

Static Methods

Static methods are methods that can be called directly. We dont need to instantiate the class before accessing them.
To create the static method we use the keyword static.Static methods can also be called from methods in other classes. To do this, the static method should be public.

<?php
class Greeting{
    public static function hello(){
        echo "Hello Tyler!";
    }
}

Greeting::hello() // we call the static method using the (::);


class A {
  public static function welcome() {
    echo "Hello World!";
  }
}

class B {
  public function message() {
    A::welcome();
  }
}

// We can also use this with arguments. 


class calc {
  // static method
  public static function sum($x, $y) {
    return $x + $y;
  }
}

// Call static method
$res = calc::sum(6, 4);
echo $res;
?>

// We can create static properties also. these properties can be accessed directly also .


class Greeting {
    public static $staticProp = "Tyler";
}
?>

// We can access the static property specify the class name the same way we are accessed the column. 

Greeting::$staticProp;

PHP NameSpaces

Namespace is used to avoid name conflicts between classes, interfaces, and functions.
 
Namespace groups related code together under a name to avoid conflicts as our code keeps growing. It helps us keep our code logical and seperates our code.

To use namespace they must be declared at the top of our code.
 
View the blueprint below :

// File 1 

<?php 
class User{
    public function getName(){
        return "Regular User";
    }
}

?>

// File 2 

<?php
 class User{
    public function getUser(){
        return "Admin User";
    }
 }

 ?>

 // This would cause problems as PHP will see duplicate names. 


 // Using NameSpace


 // File 1
//  Folder - App/User.php

 <?php 

namespace App;
class User{
    public function getUser(){
        return "Regular User";
    }
}

?>

//File 2

//  Folder - Admin/User.php
 <?php 

namespace Admin;
class User{
    public function getUser(){
        return "Regular User";
    }
}

?>

// Using the Classes in our current file (index.php)

<?php 

require 'App/User.php';
require 'Admin/User.php';

use App\User;
use Admin\User as AdminUser;

$user1 = new User();
$user2 = new AdminUser();

echo $user1->getUser();
echo $user2->getUser();
?>