PHP Constructor
A constructor in PHP is a special type of function that is automatically called when an object of a class is created. Constructors are typically used to initialize properties of a class when an object is instantiated.
Here's a simple example to illustrate the concept of constructors in PHP:
Example 1: Basic Constructor
<?php
class Car {
public $make;
public $model;
// Constructor
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
public function getDetails() {
return "Make: " . $this->make . ", Model: " . $this->model;
}
}
// Create an object of Car
$car1 = new Car("Toyota", "Corolla");
echo $car1->getDetails(); // Output: Make: Toyota, Model: Corolla
?>
In this example, the Car
class has a constructor method __construct
that takes two parameters: $make
and $model
. When a Car
object is created, the constructor is called with the specified arguments, and the properties $make
and $model
are initialized.
Example 2: Constructor with Default Values
You can also define default values for the constructor parameters:
<?php
class Car {
public $make;
public $model;
// Constructor with default values
public function __construct($make = "Unknown", $model = "Unknown") {
$this->make = $make;
$this->model = $model;
}
public function getDetails() {
return "Make: " . $this->make . ", Model: " . $this->model;
}
}
// Create an object of Car without arguments
$car2 = new Car();
echo $car2->getDetails(); // Output: Make: Unknown, Model: Unknown
// Create an object of Car with arguments
$car3 = new Car("Honda", "Civic");
echo $car3->getDetails(); // Output: Make: Honda, Model: Civic
?>
In this example, if no arguments are provided when creating a Car
object, the constructor will use the default values "Unknown" for both $make
and $model
.
Example 3: Using Constructors with Inheritance
When dealing with inheritance, the child class can call the parent class constructor using parent::__construct()
.
<?php
class Vehicle {
public $make;
// Parent constructor
public function __construct($make) {
$this->make = $make;
}
}
class Car extends Vehicle {
public $model;
// Child constructor
public function __construct($make, $model) {
parent::__construct($make); // Call the parent constructor
$this->model = $model;
}
public function getDetails() {
return "Make: " . $this->make . ", Model: " . $this->model;
}
}
// Create an object of Car
$car4 = new Car("Ford", "Focus");
echo $car4->getDetails(); // Output: Make: Ford, Model: Focus
?>
In this example, the Car
class inherits from the Vehicle
class. The constructor of the Car
class calls the constructor of the Vehicle
class using parent::__construct($make)
to initialize the make
property.
These examples show how constructors are used in PHP to initialize objects and manage inheritance.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.