PHP
Initializing properties
Learn to assign initial values to properties in the constructor.
Initializing properties in the constructor
The most common use of the constructor is to give the class's properties their initial values. You do this by assigning the parameter to $this->propertyName.
class User {
public string $name;
public function __construct($name) {
$this->name = $name;
}
}
Here the $name parameter from the constructor is copied to the $this->name property on the instance. This way each instance carries its own name and other methods can read it through $this->name.
Even if the parameter and the property share a name, PHP keeps them apart: $name is the parameter and $this->name is the instance property.
Property declaration and initialization
Before initializing properties in the constructor, you must declare them inside the class. The declaration goes at the top of the class and includes the visibility and the type:
class Account {
public string $owner;
public float $balance;
}
Inside the constructor you assign each property, for example $this->owner = $owner;. If you assign without declaring the property first, PHP warns about a dynamic property; the safe order is to declare first, then initialize.
You may also give a default value in the declaration: public float $balance = 0.0;. But initializing in the constructor is usually more flexible.
Exercise Initialize properties in the Book class
Define a class named Book. Declare properties public string $title; and public string $author; inside the class. The constructor should take two parameters with the signature __construct($title, $author) and in the body perform $this->title = $title; and $this->author = $author;. Below the class create an instance with $b = new Book("Crime and Punishment", "Dostoevsky"); and print it with echo $b->title . " - " . $b->author . "\n";.
Show example solution
At the top of the class write public string $title; and public string $author;. In the constructor body $this->title = $title; and $this->author = $author;. Then $b = new Book("Crime and Punishment", "Dostoevsky"); and echo $b->title . " - " . $b->author . "\n";.
Example solution:
<?php
// Define the Book class, declare properties and initialize in constructor
class Book {
public string $title;
public string $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
}
$b = new Book("Crime and Punishment", "Dostoevsky");
echo $b->title . " - " . $b->author . "\n";
Test yourself
-
What does $this->name = $name do?
- Copies the parameter into the instance property
- Copies the instance property into the parameter
- Concatenates two parameters
- Declares a new local variable
Show answer
Copies the parameter into the instance property — Correct. $this->name is the instance property and $name is the parameter. Assignment goes right to left.
-
Match the concepts
- $this->name
- instance property
- $name
- constructor parameter
- public string $name
- property declaration
- $this->name = $name
- assignment in constructor
-
Complete the assignment line
public function __construct($owner) {
____;
}
Show answer
public function __construct($owner) {
$this->owner = $owner;
}
Next lesson: Class type declaration