OnlineCBT Logo OnlineCBT
PHP · LESSON 16

Object-Oriented PHP: Classes, Objects, and Access Modifiers

By Ratan Updated: 16 Oct 2026

Build object-oriented applications using class syntax, public/private/protected visibility, and $this.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Core Concepts of Object-Oriented Programming (OOP)

OOP is a programming paradigm based on the concept of "objects", which contain data (attributes/properties) and code (methods/behaviors).

  • Class: A blueprint or template defining the structure and capabilities of an entity (e.g., Course, Student).
  • Object: A concrete instance created from a class (e.g., $neetCourse = new Course();).

2. Class Declaration and Instantiation

name} ({$this->email})";
    }
}

// Instantiating an object
$user1 = new User();
$user1->name = "Amit Kumar";
$user1->email = "amit@cbt.com";

echo $user1->getDetails();

3. Visibility / Access Modifiers

Encapsulation hides the internal representation of an object and protects data from unintended direct modification:

Modifier Access From Inside Class Access From Child Classes Access From Outside Code
public Yes Yes Yes
protected Yes Yes No
private Yes No No
 0) {
            $this->balance += $amount;
        }
    }

    public function getBalance(): float {
        return $this->balance;
    }
}

$acc = new BankAccount();
$acc->deposit(500);
echo $acc->getBalance(); // 500
// $acc->balance = 10000; // Fatal Error: Cannot access private property

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat