If you’re used to other object oriented programming languages, you’re probably familiar with the constructor. You may also be familiar with the constructor being named as the class name; it’s just a method that shares the same name as the class name.
In PHP, it’s actually simplified in my opinion. You name the constructor method, construct with two underscores before it: __construct(). Great, but what is the point of the constructor. It’s there to initialize properties inside the object upon instantiation; PHP calls the constructor method automatically when the object is instantiated. Let’s look at some code to see the constructor in detail.
If you haven’t been following along the PHP article series, I have 48 other articles written before this one. To grab the code and follow along, check out the previous article on Type Declarations.
https://www.dinocajic.com/php-type-declarations/
Evaluating the code below, you’ll notice that all of our properties have been initialized for our GermanShepherd class. This is great for common properties like the $kingdom and $breed, however, not all German Shepherds share the same date of birth.
<?php
class GermanShepherd {
const HAS_HEART = true;
const HAS_TAIL = true;
public $eye_color = "Brown";
public $dob = "January 30, 2017";
public $does_shed = true;
public $kingdom = "Animalia";
public $phylum = "Chordata";
public $class = "Mammalia";
public $order = "Carnivara";
public $family = "Canidae";
public $genus = "Canis";
public $species = "Canis Lupus";
public $subspecies = "Canis Lupus Familiaris";
public $breed = "German Shepherd Dog";
public $fur_color = "Black and Tan";
}
Before we make the modification, let’s add a blank constructor to the class.
<?php
// What you know about $this?
class GermanShepherd {
// ...
public $eye_color = "Brown";
// ...
public function __construct() {
}
}
We want to pass arguments to the constructor on object instantiation. The constructor is the method that’s called first, before anything else happens. The properties that need to be initialized on object instantiation are:
- Eye Color
- Date of Birth
- Fur Color
Just like a normal method, we’ll start off by declaring our parameters.
<?php
class GermanShepherd {
public $eye_color;
public $dob;
public $fur_color;
public function __construct( string $eye_color, string $dob, string $fur_color ) {
}
}
Next, we need to set our $eye_color, $dob, and $fur_color properties with the arguments that are passed. Remember, to access our properties inside of the object, we’ll need the help of $this keyword.
For this example, I chose to name the parameters the exact same way as our properties. The $this keyword helps to differentiate whether we’re accessing the property or the parameter value.
<?php
class GermanShepherd {
public $eye_color;
public $dob;
public $fur_color;
public function __construct( string $eye_color, string $dob, string $fur_color )
{
$this->eye_color = $eye_color;
$this->dob = $dob;
$this->fur_color = $fur_color;
}
}
You can name your parameters whatever you’d like. Just make sure to make the modification in the method body as well.
<?php
class GermanShepherd {
public $eye_color;
public $dob;
public $fur_color;
public function __construct( string $x, string $y, string $z )
{
$this->eye_color = $x;
$this->dob = $y;
$this->fur_color = $z;
}
}
Even though the code above works, it’s not ideal since it’s not descriptive enough. We’ll go back to the previous naming convention for the remainder of the article. Now that we have our constructor created, let’s actually put it to use.
How do you pass arguments to a constructor? You’ve instantiated objects without constructors so far, so let’s see how that’s done.
$gs = new GermanShepherd();
To pass the arguments to the constructor, add them in between the parentheses after the class name.
$gs = new GermanShepherd(“Brown”, “Feb 20, 2019”, “Black);
Make sure that you’re passing the arguments in the correct order, the order that the parameters were defined in.
<?php
class GermanShepherd {
public $eye_color;
public $dob;
public $fur_color;
public function __construct( string $eye_color, string $dob, string $fur_color )
{
$this->eye_color = $eye_color;
$this->dob = $dob;
$this->fur_color = $fur_color;
}
public function get_eye_color(): string
{
return $this->eye_color;
}
}
$gs_dog = new GermanShepherd( "Brown", "Feb 20, 2019", "Black" );
echo $gs_dog->get_eye_color();
The output of the code above will be Brown.
Looking at our Car class, the constructor becomes super useful. The Car is an abstract concept, not an actual object. We’ll cover Abstract Classes later.
<?php
class Car {
public string $color;
public string $make;
public string $model;
public int $year;
public int $fuel_type;
public int $hp;
public int $tq;
public string $transmission;
public string $vehicle_type;
public float $exterior_height;
public float $exterior_width;
public float $exterior_length;
public string $exterior_um;
public float $weight;
public string $weight_um;
public bool $car_on;
}
As you can see above, we have quite a number of properties that need to be initialized on object instantiation.
<?php
class Car {
public string $color;
public string $make;
public string $model;
public int $year;
public int $fuel_type;
public int $hp;
public int $tq;
public string $transmission;
public string $vehicle_type;
public float $exterior_height;
public float $exterior_width;
public float $exterior_length;
public string $exterior_um;
public float $weight;
public string $weight_um;
public bool $car_on;
public function __construct( int $year,
string $make,
string $model,
string $color,
int $fuel_type,
int $hp,
int $tq,
string $transmission,
string $vehicle_type,
float $exterior_height,
float $exterior_width,
float $exterior_length,
string $exterior_um,
float $weight,
string $weight_um,
bool $car_on
)
{
$this->year = $year;
$this->make = $make;
$this->model = $model;
$this->color = $color;
$this->fuel_type = $fuel_type;
$this->hp = $hp;
$this->tq = $tq;
$this->transmission = $transmission;
$this->vehicle_type = $vehicle_type;
$this->exterior_height = $exterior_height;
$this->exterior_width = $exterior_width;
$this->exterior_length = $exterior_length;
$this->exterior_um = $exterior_um;
$this->weight = $weight;
$this->weight_um = $weight_um;
$this->car_on = $car_on;
}
}
This would be quite painful to instantiate every single time, especially if you wanted to create a car quickly. We could only require a small portion of parameters to be passed and make the others optional. The modification that we need to make is simple: set default values to parameters that you want to mark as optional.
<?php
class Car {
public string $color;
public string $make;
public string $model;
public int $year;
public int $fuel_type;
public int $hp;
public int $tq;
public string $transmission;
public string $vehicle_type;
public float $exterior_height;
public float $exterior_width;
public float $exterior_length;
public string $exterior_um;
public float $weight;
public string $weight_um;
public bool $car_on;
public function __construct( int $year,
string $make,
string $model,
string $color = "",
int $fuel_type = 93,
int $hp = -1,
int $tq = -1,
string $transmission = "5 Speed Manual",
string $vehicle_type = "",
float $exterior_height = -1,
float $exterior_width = -1,
float $exterior_length = -1,
string $exterior_um = "in",
float $weight = -1,
string $weight_um = "lbs",
bool $car_on = false
)
{
$this->year = $year;
$this->make = $make;
$this->model = $model;
$this->color = $color;
$this->fuel_type = $fuel_type;
$this->hp = $hp;
$this->tq = $tq;
$this->transmission = $transmission;
$this->vehicle_type = $vehicle_type;
$this->exterior_height = $exterior_height;
$this->exterior_width = $exterior_width;
$this->exterior_length = $exterior_length;
$this->exterior_um = $exterior_um;
$this->weight = $weight;
$this->weight_um = $weight_um;
$this->car_on = $car_on;
}
}
The only thing to remember when setting optional parameters is that they should all go to the end. In the example above, we have the $year, $make, and $model as the only required parameters. If we moved $model after the $color, then the color would be required, even though it has a default value. To keep it simple, move your mandatory parameters to the front. For testing, I’ve created a new method that returns a few of our properties in an associative array and I’ve created the Nissan Skyline GTR object.
<?php
class Car {
public string $color;
public string $make;
public string $model;
public int $year;
public int $fuel_type;
public int $hp;
public int $tq;
public string $transmission;
public string $vehicle_type;
public float $exterior_height;
public float $exterior_width;
public float $exterior_length;
public string $exterior_um;
public float $weight;
public string $weight_um;
public bool $car_on;
public function __construct( int $year,
string $make,
string $model,
string $color = "",
int $fuel_type = 93,
int $hp = -1,
int $tq = -1,
string $transmission = "5 Speed Manual",
string $vehicle_type = "",
float $exterior_height = -1,
float $exterior_width = -1,
float $exterior_length = -1,
string $exterior_um = "in",
float $weight = -1,
string $weight_um = "lbs",
bool $car_on = false
)
{
$this->year = $year;
$this->make = $make;
$this->model = $model;
$this->color = $color;
$this->fuel_type = $fuel_type;
$this->hp = $hp;
$this->tq = $tq;
$this->transmission = $transmission;
$this->vehicle_type = $vehicle_type;
$this->exterior_height = $exterior_height;
$this->exterior_width = $exterior_width;
$this->exterior_length = $exterior_length;
$this->exterior_um = $exterior_um;
$this->weight = $weight;
$this->weight_um = $weight_um;
$this->car_on = $car_on;
}
public function get_car_details(): array
{
return [
"year" => $this->year,
"make" => $this->make,
"model" => $this->model,
"color" => $this->color,
"fuel_type" => $this->fuel_type,
"hp" => $this->hp,
"torque" => $this->tq,
"transmission" => $this->transmission
];
}
}
$nissan = new Car(
1999,
"Nissan",
"Skyline GTR",
"Black",
93,
-1,
1000
);
var_dump( $nissan->get_car_details() );
The GTR creation starts off as expected. We add the year, make and model since those are our required properties. I do want to add the color and torque as well. Color is simple since it comes next in line, but the torque comes after the fuel type and horsepower specifications. You will have to pass the default values to the constructor if you want to keep them as default values, as is shown above. If we skipped adding the -1 for horsepower, 1000 would be labeled as horsepower, and that’s not intended.
Our output shows the transmission as well. Even though we didn’t pass it in the constructor, it did have a default value set to it, therefore the transmission is displayed as a 5 Speed Manual.