laravel - In PHP how do I type hint an instance of a class accessed using a string as the class name
Using Laravel, I'm iterating through a series of classes and doing an operation on a subset of instances of each class. The classes are provided as strings, eg:
$c = '\App\Models\Book';
$c::each(function($i) {
echo $i->title . PHP_EOL;
});
How would I type hint $i
?
Answer
Solution:
You can type it as object{title:string}
, if you sure about title
and wants to tell about it to paslm
.
Also, maybe you may find useful conditional types - https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/
Source