Bird
0
0

What will happen if you forget to type hint a dependency in a Laravel controller constructor but try to use it inside a method?

medium📝 component behavior Q5 of 15
Laravel - Controllers
What will happen if you forget to type hint a dependency in a Laravel controller constructor but try to use it inside a method?
ALaravel will throw a binding resolution exception at runtime.
BLaravel will automatically create the dependency without type hint.
CThe controller will work fine without errors.
DThe dependency will be null silently.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Laravel's dependency resolution

    Laravel resolves dependencies by type hinting. Without it, Laravel cannot inject the dependency.
  2. Step 2: Identify runtime behavior

    When the dependency is missing, Laravel throws a binding resolution exception indicating it cannot resolve the class.
  3. Final Answer:

    Laravel will throw a binding resolution exception at runtime. -> Option A
  4. Quick Check:

    Missing type hint = Binding resolution exception [OK]
Quick Trick: Always type hint dependencies to avoid runtime errors [OK]
Common Mistakes:
  • Assuming Laravel auto-creates unhinted dependencies
  • Expecting silent null values
  • Thinking no error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes