PHP - Typed Properties in PHP
Typed properties are a feature introduced in PHP 7.4 that allow developers to explicitly define the data type of class properties. Before this feature, properties in PHP classes could hold values of any type unless manually validated. Typed properties bring stricter type safety, better code clarity, and fewer runtime errors.
What Are Typed Properties
A typed property is a class property declared with a specific data type. This means the property can only store values of that declared type. If a different type is assigned, PHP will throw a TypeError.
Example:
class User {
public string $name;
public int $age;
}
In this example:
-
$namemust always be a string -
$agemust always be an integer
Initialization Rules
Typed properties must follow strict initialization rules:
-
They cannot be accessed before being initialized.
-
If accessed without initialization, PHP throws an error.
Example:
class Product {
public int $price;
}
$product = new Product();
echo $product->price; // Error: must not be accessed before initialization
To avoid this, you can initialize properties:
class Product {
public int $price = 0;
}
Nullable Typed Properties
Sometimes a property may or may not have a value. In such cases, nullable types are used by prefixing the type with a question mark.
Example:
class Order {
public ?string $status = null;
}
Here, $status can either be a string or null.
Supported Data Types
Typed properties support most common PHP data types:
-
int
-
float
-
string
-
bool
-
array
-
object
-
callable
-
iterable
-
self
-
parent
-
class/interface names
Example:
class Blog {
public array $posts;
public bool $isPublished;
}
Type Enforcement
PHP strictly enforces types at runtime. Assigning an invalid type results in an error.
Example:
class Test {
public int $number;
}
$obj = new Test();
$obj->number = "ten"; // TypeError
This helps catch bugs early and improves reliability.
Default Values
Typed properties can have default values, but they must match the declared type.
Valid example:
public int $count = 10;
Invalid example:
public int $count = "ten"; // Error
Difference from Untyped Properties
Before typed properties:
class Example {
public $value;
}
This allows any type:
$obj->value = 10;
$obj->value = "text";
$obj->value = [];
With typed properties:
class Example {
public int $value;
}
Only integers are allowed, ensuring consistency.
Benefits of Typed Properties
-
Improves type safety by preventing invalid data assignments
-
Makes code more readable and self-documenting
-
Reduces debugging time by catching errors early
-
Helps IDEs provide better autocomplete and analysis
-
Encourages modern programming practices
Limitations
-
Cannot use
voidorresourceas property types -
Must be initialized before use
-
Strict typing may require additional handling in dynamic scenarios
Practical Use Case
Consider a user management system:
class User {
public int $id;
public string $email;
public bool $isActive;
}
This ensures:
-
IDs are always integers
-
Emails are always strings
-
Status is always boolean
Such strict structure improves reliability in large applications.
Conclusion
Typed properties are a significant improvement in PHP that align it with modern strongly typed programming practices. By enforcing data types at the property level, they reduce runtime errors, improve maintainability, and make codebases more predictable and robust.