OOP stands for Object-Oriented Programming, which is a programming paradigm based on the concept of "objects". PHP supports OOP, and over the years, it has been enhanced to include more features and support for modern OOP practices. Here's an overview of the main concepts of OOP and how they are implemented in PHP:
Classes and Objects:
class Dog {
public $name;
public function bark() {
echo "Woof! Woof!";
}
}
$dog1 = new Dog();
$dog1->name = "Buddy";
$dog1->bark(); // Outputs "Woof! Woof!"
Properties and Methods:
class Car {
public $color; // property
public function drive() { // method
echo "Car is driving";
}
}
Encapsulation:
class Wallet {
private $money;
public function __construct($initialAmount) {
$this->money = $initialAmount;
}
public function spend($amount) {
if ($amount <= $this->money) {
$this->money -= $amount;
return true;
}
return false;
}
public function checkBalance() {
return $this->money;
}
}
Inheritance:
class Vehicle {
public $speed = 0;
public function accelerate() {
$this->speed += 10;
}
}
class Car extends Vehicle {
public $wheels = 4;
}
$car = new Car();
$car->accelerate();
echo $car->speed; // Outputs "10"
Polymorphism:
interface Shape {
public function area();
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
class Square implements Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function area() {
return $this->side * $this->side;
}
}
Abstraction:
abstract class Animal {
abstract public function sound();
}
class Dog extends Animal {
public function sound() {
echo "Woof! Woof!";
}
}
class Cat extends Animal {
public function sound() {
echo "Meow! Meow!";
}
}
Composition:
class Engine {
public function start() {
echo "Engine started.";
}
}
class Car {
private $engine;
public function __construct() {
$this->engine = new Engine();
}
public function start() {
$this->engine->start();
echo "Car is running.";
}
}
Constructor & Destructor:
class Book {
public $title;
public function __construct($title) {
$this->title = $title;
echo "A new book: {$this->title}";
}
public function __destruct() {
echo "The book '{$this->title}' is being destroyed.";
}
}
$book = new Book("Harry Potter");
unset($book); // Destructor will be called here
This is a general overview of OOP concepts in PHP. There's a lot more to explore in each topic, especially when you get into design patterns, advanced uses of interfaces, traits, and other OOP features. If you're looking to dive deeper, I'd recommend a comprehensive OOP-based PHP course or a dedicated book on the subject.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here