Summary: in this tutorial, you wil learn how to define typed properties by adding type hints to class properties.
Introduction to the PHP typed properties #
The following example defines the BankAccount
class with a property called $balance
:
class BankAccount
{
public $balance;
}
Code language: PHP (php)
The default value of the $balance
property is null
.
$account = new BankAccount();
var_dump($account->balance); // null
Code language: PHP (php)
PHP 7.4 allows you to type hints the class properties with any types except void
and callable
. For example:
class BankAccount
{
public float $balance;
}
Code language: PHP (php)
In this example, the $balance
property has the type float
. When you add the float
type to $balance
property, the following code causes an error:
$account = new BankAccount();
var_dump($account->balance);
Code language: PHP (php)
Error:
Fatal error: Uncaught Error: Typed property BankAccount::$balance must not be accessed before initialization
Code language: plaintext (plaintext)
It doesn’t work because the $balance
property now becomes uninitialized. The default value of the $balance
property is not null
like before.
Notice that you can still create a new object with typed properties uninitialized.
To read from a typed property, you need to initialize it first. For example:
<?php
class BankAccount
{
public float $balance;
}
$account = new BankAccount();
$account->balance = 0;
print($account->balance); // 0
Code language: PHP (php)
For properties with scalar types, you can initialize them in the declaration. For example:
<?php
class BankAccount
{
public float $balance = 0;
}
$account = new BankAccount();
print($account->balance); // 0
Code language: PHP (php)
Alternatively, you can initialize the typed properties in the constructor of the class:
<?php
class BankAccount
{
public float $balance = 0;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
$account = new BankAccount(100);
print($account->balance); // 100
Code language: PHP (php)
If you unset a typed property, its status will change back to uninitialized. Note that for an untyped property, its value will become null
after unset. For example:
<?php
class BankAccount
{
public float $balance = 0;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
$account = new BankAccount(0);
echo $account->balance; // 0
unset($account->balance);
echo $account->balance; // error
Code language: PHP (php)
Typed properties and strict types #
In the following example, the constructor of the BankAccount
expects a float
. However, you can pass a string. In this case, PHP coerces the string to a float:
<?php
class BankAccount
{
public float $balance = 0;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
$account = new BankAccount("100.5");
echo $account->balance; // 100.5
Code language: PHP (php)
If you don’t want this behavior, you can disable it by declaring strict_types
at the beginning of the file as follows:
<?php
declare(strict_types=1);
class BankAccount
{
public float $balance = 0;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
$account = new BankAccount("100.25"); // error
var_dump($account->balance);
Code language: PHP (php)
Error:
Fatal error: Uncaught TypeError: Argument 1 passed to BankAccount::__construct() must be of the type float, string given.
Code language: plaintext (plaintext)
Summary #
- Typed properties include modifiers (
private
,protected
, andpublic
) and types (exceptvoid
andcallable
). - Typed properties have uninitialized states, not null like untyped properties.