Q1: What are the key features of PHP 8.0, 8.1, and 8.2 that you use daily?
- Constructor Property Promotion (8.0): Reduces boilerplate in classes/DTOs.
- Named Arguments (8.0): clearer function calls, skipping optional params.
- Match Expression (8.0): Strict, expression-based alternative to switch.
- Enums (8.1): First-class support for enumerations.
- Readonly Properties/Classes (8.1/8.2): Immutable data objects (DTOs).
readonly class UserData {
public function __construct(
public string $name,
public UserStatus $status, // Enum
) {}
}
Q2: Explain the Laravel Service Container and Dependency Injection.
Service Container: A powerful tool for managing class dependencies and performing dependency injection. It acts as a registry where you bind interfaces to implementations.
Dependency Injection: Instead of creating objects manually (new Service()), you type-hint them in the constructor, and the container resolves them automatically.
Why? Decoupling. It allows you to swap implementations (e.g., switching from Stripe to PayPal) without changing the consuming code, and makes testing easier by allowing mocks.
Q3: What is the difference between bind, singleton, and scoped?
- bind: Returns a new instance every time the service is requested.
- singleton: Returns the same instance (created once) for the entire lifetime of the application (or request in web context).
- scoped: Similar to singleton, but flushed whenever the Laravel application starts a new "scope" (relevant for Octane/long-running processes).
Q4: Describe the Laravel Request Lifecycle.
- Entry:
public/index.phploads Composer autoloader. - Kernel: Request is sent to HTTP Kernel. Global middleware runs.
- Service Providers:
register()(bindings) thenboot()(listeners, routes) methods are called. - Router: Dispatches request to Controller or Closure. Route middleware runs.
- Controller: Executes logic, returns Response.
- Output: Response is sent back through middleware to the browser.
Q5: What are Traits and how are they used in Laravel?
Traits: A mechanism for code reuse in single inheritance languages like PHP. They allow you to "copy and paste" methods into multiple classes.
Laravel Examples:
Notifiable: Adds email/notification methods to the User model.SoftDeletes: Adds logic to handle "trash" functionality.HasFactory: Enables model factories for testing.
Q6: Explain the difference between `abstract class` and `interface`.
- Interface: A contract. It defines what methods a class must implement, but not how. A class can implement multiple interfaces.
- Abstract Class: A blueprint. It can provide some implementation (shared code) while forcing child classes to implement specific methods. A class can only extend one abstract class.
Q7: What are Generators in PHP?
Concept: Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.
Keyword: yield.
Benefit: Memory efficiency. Instead of loading a million records into an array in memory, a generator yields one record at a time.
function getLines($file) {
$f = fopen($file, 'r');
while ($line = fgets($f)) {
yield $line;
}
fclose($f);
}