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