PHP - Classes and Objects
Welcome, aspiring programmers! Today, we're diving into the exciting world of PHP classes and objects. Don't worry if these terms sound a bit intimidating – by the end of this lesson, you'll be creating your own classes and objects like a pro!
What are Classes and Objects?
Before we jump into the code, let's understand what classes and objects are. Imagine you're building a house. The class would be like the blueprint for the house, while an object would be the actual house built from that blueprint. You can create many houses (objects) from a single blueprint (class).
Defining a Class in PHP
Let's start by creating our first PHP class. We'll create a simple Car
class.
<?php
class Car {
// Class properties
public $brand;
public $color;
// Class method
public function startEngine() {
return "Vroom! The $this->color $this->brand is starting.";
}
}
?>
Let's break this down:
- We start with the
class
keyword, followed by the name of our class (in this case,Car
). - Inside the class, we define properties (variables) like
$brand
and$color
. - We also define a method (function) called
startEngine()
.
Creating Objects from a Class
Now that we have our Car
class, let's create some car objects!
<?php
// Creating car objects
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Red";
$friendsCar = new Car();
$friendsCar->brand = "Honda";
$friendsCar->color = "Blue";
// Using the object's method
echo $myCar->startEngine();
echo $friendsCar->startEngine();
?>
Output:
Vroom! The Red Toyota is starting.
Vroom! The Blue Honda is starting.
Here's what's happening:
- We use the
new
keyword to create objects from ourCar
class. - We set the properties of each object using the arrow (
->
) operator. - We call the
startEngine()
method on each object.
Constructor Method
Now, let's make our class a bit more sophisticated by adding a constructor. A constructor is a special method that's called when an object is created.
<?php
class Car {
public $brand;
public $color;
// Constructor
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function startEngine() {
return "Vroom! The $this->color $this->brand is starting.";
}
}
// Creating car objects with constructor
$myCar = new Car("Toyota", "Red");
$friendsCar = new Car("Honda", "Blue");
echo $myCar->startEngine();
echo $friendsCar->startEngine();
?>
The constructor allows us to set the brand
and color
when we create the object, making our code cleaner and more efficient.
Inheritance
One of the powerful features of OOP is inheritance. It allows us to create a new class based on an existing class. Let's create an ElectricCar
class that inherits from our Car
class.
<?php
class ElectricCar extends Car {
public $batteryLife;
public function __construct($brand, $color, $batteryLife) {
parent::__construct($brand, $color);
$this->batteryLife = $batteryLife;
}
public function chargeBattery() {
return "The $this->color $this->brand is charging. Current battery life: $this->batteryLife%";
}
}
$teslaModel3 = new ElectricCar("Tesla", "White", 80);
echo $teslaModel3->startEngine();
echo $teslaModel3->chargeBattery();
?>
Output:
Vroom! The White Tesla is starting.
The White Tesla is charging. Current battery life: 80%
Here, ElectricCar
inherits all the properties and methods of Car
, and adds its own property ($batteryLife
) and method (chargeBattery()
).
Access Modifiers
In OOP, we can control the visibility of properties and methods using access modifiers. There are three types:
Modifier | Description |
---|---|
public | Accessible from anywhere |
protected | Accessible within the class and by derived classes |
private | Accessible only within the class |
Let's modify our Car
class to use these:
<?php
class Car {
protected $brand;
private $engineStarted = false;
public function __construct($brand) {
$this->brand = $brand;
}
public function startEngine() {
$this->engineStarted = true;
return "The $this->brand's engine is now running.";
}
public function isEngineRunning() {
return $this->engineStarted;
}
}
$myCar = new Car("Toyota");
echo $myCar->startEngine();
echo $myCar->isEngineRunning() ? "Engine is running" : "Engine is off";
// This would cause an error: echo $myCar->engineStarted;
?>
Now, $brand
is protected (accessible in child classes), $engineStarted
is private (only accessible within the Car
class), and our methods remain public.
Conclusion
Congratulations! You've just taken your first steps into the world of Object-Oriented Programming with PHP. We've covered classes, objects, constructors, inheritance, and access modifiers. These concepts form the foundation of OOP and will serve you well as you continue your programming journey.
Remember, practice makes perfect. Try creating your own classes, experiment with different properties and methods, and don't be afraid to make mistakes – that's how we learn! Happy coding!
Credits: Image by storyset