Interview Q&A · questions with model answers

PHP 8 Interview Questions

Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.

  1. Most asked Explain readonly properties (8.1) and readonly classes (8.2).

    A readonly property can be initialized once, from inside the declaring class scope, and never modified afterwards. A readonly class makes every declared property readonly.

  2. Most asked How does Composer autoloading work, and what is PSR-4?

    PSR-4 maps a namespace prefix to a base directory so App\Models\User loads from src/Models/User.php. Composer generates an autoloader from composer.json that requires files on first use.

  3. Most asked How should passwords be stored in PHP?

    Use password_hash() (bcrypt by default, or Argon2id) and password_verify(), and call password_needs_rehash() on login to upgrade old hashes.

  4. Most asked What happens to an HTTP request in a PHP-FPM application, and what does 'shared nothing' mean?

    nginx passes the request to PHP-FPM over FastCGI; the master assigns it to an idle worker process; the worker runs the entry script (public/index.php) from the top: autoloading, framework bootstrap,…

  5. Most asked How does the PHP 8 JIT work and when does it improve performance?

    The JIT compiles hot opcodes to native machine code at runtime on top of OPcache. It greatly speeds up CPU-bound work (math, image processing), but gives little benefit to typical I/O-bound web apps.

  6. Most asked Explain the SOLID principles with PHP examples.

    SOLID stands for Single responsibility, Open/closed, Liskov substitution, Interface segregation and Dependency inversion, five guidelines for maintainable OO design.

  7. Explain covariance and contravariance in PHP method signatures.

    A child method may return a more specific type (covariant return) and may accept a more general parameter type (contravariant parameters). This has been fully supported since PHP 7.4.

  8. How does PSR-15 middleware work?

    A PSR-15 middleware implements process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface, so it can act before and after passing the request down the pipeline.

  9. How do you handle file uploads securely in PHP?

    Validate size and real MIME type (via finfo), generate your own file name, store outside the web root (or in object storage), and never trust the client-provided name or extension.

  10. How do generators help with memory usage?

    Generators (yield) produce values lazily one at a time, so you can iterate huge datasets (files, DB cursors) without loading everything into memory.

  11. How does PHP handle copying arrays and objects (copy-on-write)?

    Arrays and strings are values with copy-on-write: assigning shares the data until one copy is modified. Objects are passed as handles, so assignment copies the handle and both variables refer to the…

  12. Describe PHP's exception hierarchy and good error-handling practices.

    Throwable is the root, with two branches: Error (engine errors like TypeError, ValueError, ArithmeticError) and Exception (application exceptions like RuntimeException, LogicException).

  13. How do PHPStan/Psalm and Rector improve a PHP codebase?

    PHPStan and Psalm find type errors and bugs without running code, at configurable strictness levels; Rector automatically refactors code, for example to upgrade PHP versions or framework APIs.

  14. Explain the Repository and Service layer patterns in PHP apps.

    A repository encapsulates data access for an aggregate behind an interface (e.g., UserRepository::findByEmail), while a service layer holds use-case logic that orchestrates repositories and domain…

  15. Explain Symfony's HttpKernel request lifecycle and its kernel events.

    The front controller boots the Kernel and calls handle(); kernel.request listeners run (session, routing, locale, security firewall, your listeners) and may return a response early;…

  16. How did array unpacking change in PHP 8.1?

    PHP 8.1 allows the spread operator ... to unpack arrays with string keys, which previously threw an error; later keys overwrite earlier ones like array_merge.

  17. How do you resolve method conflicts between two traits?

    Use the insteadof operator to choose which trait's method wins, and as to alias the other one (optionally changing its visibility).

  18. Explain closures, `use`, arrow functions and `Closure::bind`.

    Anonymous functions capture outer variables explicitly with use (by value, or by reference with &). Arrow functions fn() => capture by value automatically. Closure::bind/bindTo change $this and scope.

  19. How does PHP's garbage collection work?

    PHP frees values primarily via reference counting; a cycle collector periodically finds and frees reference cycles (e.g., parent ↔ child objects) that refcounting alone can't free.

  20. How do you handle Unicode strings correctly in PHP?

    PHP strings are byte arrays, so use mb_* functions (with UTF-8) for length, substrings and case conversion, and the intl extension for normalization, collation and locale formatting.

  21. How do you handle PHP sessions across multiple servers?

    Store sessions in a shared backend such as Redis or a database (via session.save_handler or framework drivers) instead of local files, or use stateless tokens.

  22. How do namespaces and `use` imports work in PHP?

    Namespaces group related code and prevent name collisions; use imports classes, functions (use function) and constants (use const) under a short name or alias.

  23. How do `ArrayAccess`, `IteratorAggregate` and `Countable` help build collection classes?

    Implementing these interfaces lets an object behave like an array: $c['key'], foreach ($c as $item) and count($c), while keeping encapsulation and type safety.

  24. Describe how PHP executes a script internally.

    PHP lexes and parses source into an AST, compiles the AST into opcodes, and the Zend VM executes the opcodes. OPcache caches the opcodes, and the JIT can further compile hot opcodes into machine code.

  25. How should a PHP app send email reliably?

    Use a mailer library (Symfony Mailer or Laravel Mail) with a transactional provider (SES, Postmark, SendGrid), send asynchronously via a queue, and configure SPF, DKIM and DMARC.

Was this page helpful?

Report a mistake