PHP supports Object-Oriented Programming (OOP), which allows you to structure your code using classes and objects. This makes your code more organized, reusable, and scalable.
🔹 What Are Classes & Objects?
Think of a class as a blueprint for creating objects. It defines properties (variables) and methods (functions) that objects of that class can use. An object is an instance of a class, meaning it’s like an actual car built from a blueprint.
Let’s look at a simple example! 🚗
📝 Example 1: Creating a Class & Object
Here’s how to define a class and create an object in PHP:
<?php
// Define a class
class Car {
public $brand; // Property
// Method to set the brand name
function setBrand($name) {
$this->brand = $name;
}
// Method to get the brand name
function getBrand() {
return $this->brand;
}
}
// Create an object (instance of the class)
$myCar = new Car();
$myCar->setBrand("Tesla");
echo "My car brand is: " . $myCar->getBrand();
?>
Here, we created a Car class with a brand property and two methods: setBrand() to set the brand name and getBrand() to retrieve it. Then, we created an object of the Car class and assigned it a brand name.
📝 Example 2: Constructor Method
A constructor is a special method that gets automatically called when an object is created. We use __construct() in PHP to define a constructor.
<?php
class Animal {
public $name;
// Constructor method
function __construct($animalName) {
$this->name = $animalName;
}
function getName() {
return "This is a " . $this->name;
}
}
// Creating objects with a constructor
$cat = new Animal("Cat");
$dog = new Animal("Dog");
echo $cat->getName(); // Output: This is a Cat
echo "<br>";
echo $dog->getName(); // Output: This is a Dog
?>
Whenever we create an object of the Animal class, the constructor automatically assigns the name.
🔹 Class Properties: Public, Private, and Protected
Properties in a class can have different access levels:
public– Can be accessed from anywhere.private– Can only be accessed inside the class.protected– Can be accessed inside the class and in child classes.
Let’s see an example:
<?php
class Person {
public $name; // Public property
private $age; // Private property
function __construct($personName, $personAge) {
$this->name = $personName;
$this->age = $personAge;
}
// Public method to access private property
function getAge() {
return "Age: " . $this->age;
}
}
$john = new Person("John", 25);
echo "Name: " . $john->name;
echo "<br>";
echo $john->getAge(); // Allowed (accesses private property through method)
// echo $john->age; // ERROR: Cannot access private property
?>
The $age property is private, so it cannot be accessed directly from outside the class.
📝 Example 3: Inheritance (Extending a Class)
In inheritance, one class can inherit the properties and methods of another class using the extends keyword.
<?php
// Parent class
class Vehicle {
protected $type;
function setType($vehicleType) {
$this->type = $vehicleType;
}
function getType() {
return "Vehicle Type: " . $this->type;
}
}
// Child class
class Bike extends Vehicle {
function ride() {
return "Riding a " . $this->type;
}
}
$myBike = new Bike();
$myBike->setType("Motorcycle");
echo $myBike->getType(); // Output: Vehicle Type: Motorcycle
echo "<br>";
echo $myBike->ride(); // Output: Riding a Motorcycle
?>
Here, the Bike class inherits the properties and methods of the Vehicle class.
🎯 Key Takeaways
- A class is a blueprint, and an object is an instance of a class.
- Use constructors to automatically initialize object properties.
- Control access to class properties with
public,private, andprotectedvisibility. - Use inheritance to extend class functionality.
📝 Practice Time!
Try modifying the examples to create your own custom classes. Maybe a Fruit class 🍎 or a Laptop class 💻? Have fun experimenting! 🚀