PHP Classes and Objects are central to object-oriented programming (OOP) in PHP. Let's break down the concept into various aspects and dive deep into each one with examples:
Object-oriented programming is a programming paradigm that uses objects and classes for organizing code. This paradigm promotes the DRY principle (Don't Repeat Yourself) and helps in writing more organized and reusable code.
A class is a blueprint for objects. Think of it as a template to create objects.
class Car {
public $color;
public $brand;
function setColor($color) {
$this->color = $color;
}
function setBrand($brand) {
$this->brand = $brand;
}
function describe() {
return "This is a " . $this->color . " " . $this->brand . ".";
}
}
An object is an instance of a class. You can create multiple objects from a single class.
$myCar = new Car();
$myCar->setColor("red");
$myCar->setBrand("Toyota");
echo $myCar->describe(); // Outputs: This is a red Toyota.
In the `Car` class above, `$color` and `$brand` are properties, while `setColor()`, `setBrand()`, and `describe()` are methods.
A constructor is a special method that gets called automatically when you create an object. It's typically used for initializing properties.
class Car {
public $color;
public $brand;
function __construct($color, $brand) {
$this->color = $color;
$this->brand = $brand;
}
function describe() {
return "This is a " . $this->color . " " . $this->brand . ".";
}
}
$myCar = new Car("blue", "Ford");
echo $myCar->describe(); // Outputs: This is a blue Ford.
class Car {
public $color;
private $engineStatus = "off";
public function startEngine() {
$this->engineStatus = "on";
}
private function stopEngine() {
$this->engineStatus = "off";
}
public function getEngineStatus() {
return $this->engineStatus;
}
}
$myCar = new Car();
$myCar->startEngine();
echo $myCar->getEngineStatus(); // Outputs: on
Here, we cannot directly access or change `$engineStatus` from outside the class or call `stopEngine()` because they are private.
Allows a class (child) to inherit properties and methods from another class (parent).
class ElectricCar extends Car {
public $batteryLevel = 100;
public function charge() {
$this->batteryLevel = 100;
}
}
$tesla = new ElectricCar();
$tesla->setColor("white");
echo $tesla->describe(); // Outputs: This is a white .
echo $tesla->batteryLevel; // Outputs: 100
This is just an introduction to the basics of PHP Classes and Objects. OOP in PHP includes other concepts like Interfaces, Polymorphism, Traits, and more. As you progress, you may want to explore these deeper aspects of OOP in PHP.
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