Type hinting - specify an array of objects
When defining a function signature in PHP, the type system expects precision. If you have a class like Foo and a method that accepts a single instance, such as function getFoo(Foo $f), passing an array directly to that function triggers a fatal error: "Argument 1 passed to getFoo() must be an instance of Foo, array given." The error message is explicit because the type hint $f declares a scalar-like expectation for a single object, not a collection.
Developers often instinctively try to resolve this by adding square brackets, similar to how they might declare arrays in older PHP versions or languages like C#. You might write function getFoo(Foo $f[]). While this syntax looks logical and suggests "an array of Foo objects," it is invalid in modern PHP. The language does not support trailing comma syntax for type hints on scalar types in function arguments in the way you are hoping. Attempting to use $f[] results in a parsing error rather than the intended behavior.
To correctly specify that a function accepts an array of Foo objects, you must use the built-in array type hint combined with the specific object type. The correct syntax is:
function getFoo(array $foos): void {
// Logic here
}
If you are using PHP 7.4 or later, you can be even more explicit by utilizing the iterable keyword if you need to support both arrays and traversables like Traversable objects:
function getFoo(iterable $foos): void {
foreach ($foos as $foo) {
// Process each Foo object
}
}
However, for strict type checking where only arrays are expected, array remains the standard. In PHP 8.0 and above, you can also use union types if you need to accept either a single Foo instance or an array of them:
function getFoo(Foo|Foo[] $input): void {
// Handle both cases
}
This approach leverages the is_a() logic implicitly. Inside the function, you can determine whether the input is a collection or a single item using is_array(). This pattern allows you to write flexible code without sacrificing type safety at the call site. For example:
function getFoo(Foo|Foo[] $input): void {
if (is_array($input)) {
foreach ($input as $foo) {
echo "Processing {$foo}";
}
} else {
echo "Processing single {$input}";
}
}
One common pitfall is assuming that type hints behave exactly like variable declarations. In a class property, you might see public array $items;. While valid, function arguments require the type hint to be placed before the variable name without square brackets. Another consideration is performance; while iterable offers flexibility for generators and iterators, using it in high-performance loops where strict arrays are guaranteed might introduce a slight overhead depending on how the engine handles the iteration protocol versus direct array access.
When integrating this into a larger application, especially with frameworks like Laravel, you often encounter services that process collections. If your service method expects an array but receives a single object from a controller, you must ensure the contract between layers is defined using union types or explicit conversion logic within the method body. This prevents runtime errors and makes the codebase more robust against accidental misuse of interfaces.
For further reading on type hints and their evolution across PHP versions, resources like https://laravelcompany.com provide insights into modern PHP practices that align with these syntactic requirements. Understanding these nuances ensures your code remains maintainable and adheres to strict typing standards expected in professional environments.