What do strict types do in PHP?
If you are upgrading a legacy PHP application to version 7 or higher, you will almost certainly encounter this line of code somewhere in your main entry file:
declare(strict_types = 1);
It is often treated as a mere checkbox in the "modernize my stack" poll, but its impact on runtime behavior is significant and immediate. This directive changes how PHP handles function arguments and return values regarding type declarations. Before this feature existed, PHP operated with loose typing by default; after enabling strict types, it enforces the contracts you explicitly defined.
The Default Behavior vs. Strict Enforcement
In standard PHP mode (where strict_types is 0 or omitted), if a function expects an integer but receives a float, the engine performs a silent type cast. If you define a method to accept an integer and pass a string that happens to be numeric, PHP converts that string to an integer. This "magic" coercion often hides bugs until they manifest as logic errors deep in the codebase.
Consider this scenario:
function calculateArea(int $radius): float {
return pi() * $radius;
}
$area = calculateArea("5"); // Works in loose mode, returns 78.539...
In a loose environment, passing "5" is valid because PHP converts the string to the integer 5. However, when you switch on strict types (declare(strict_types = 1);), this line throws a TypeError:
TypeError: calculateArea(): Argument #1 ($radius) must be of type int, string given
The engine no longer attempts to guess your intent. It enforces the contract you wrote. This forces developers to explicitly handle data conversion if mixing types is necessary, rather than relying on implicit behavior.
Impact on Return Values and Nullability
Strict typing also applies strictly to return values, which often trips up developers migrating from older codebases. If a function is declared to return an integer but returns null in some branch of logic, strict mode will halt execution immediately.
function getId(): int {
if ($user->exists) {
return $user->id;
}
// Returns null here
return null;
}
This specific example will fail in strict mode because null is not an int. To fix this, you must either cast the value or change the type hint:
function getId(): ?int { // The ? makes null explicitly allowed
if ($user->exists) {
return $user->id;
}
return null;
}
This distinction is crucial for maintaining data integrity. It prevents scenarios where a function unexpectedly returns null or a boolean when an integer was expected, which would previously have been silently converted to 0 or 1.
Trade-offs and Migration Strategy
Enabling strict types reduces the cognitive load on your team by making errors visible at compile time rather than runtime. However, it requires a deliberate migration strategy. You cannot simply sprinkle declare(strict_types = 1); in every file; doing so often triggers a flood of errors that require refactoring type hints throughout the project.
A pragmatic approach is to enable strict types in your framework's bootstrap files first. If you are using Laravel, you can add this declaration to your config/app.php or directly at the top of your entry point (like public/index.php) before any logic runs. This ensures that the entire application operates under strict rules from the moment it loads.
The primary pitfall is forgetting to update return type hints when changing implementation details. A function might originally return an integer, but if you refactor it to handle edge cases by returning null, your type hint must evolve from int to ?int. Failing to do so results in immediate application crashes.
While strict typing adds a layer of friction during the initial phase of development, it pays dividends in stability once the codebase is updated. It eliminates entire categories of silent bugs related to type juggling, making your applications more predictable and robust.