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 What are the most important features introduced in PHP 8.0?

    PHP 8.0 added the JIT compiler, union types, named arguments, attributes, constructor property promotion, the match expression, the nullsafe operator and much stricter, more consistent error handling.

  2. Most asked What are union types in PHP 8 and how do you use them?

    Union types let a parameter, return value or property accept one of several types, written TypeA|TypeB, and are enforced by the engine at runtime.

  3. Most asked What are named arguments and what are their benefits and risks?

    Named arguments let callers pass values by parameter name (fn(name: 'x')), so they can skip optional parameters and make call sites self-documenting.

  4. Most asked What is constructor property promotion?

    Constructor promotion lets you declare and assign class properties directly in the constructor signature by adding a visibility modifier to the parameter.

  5. Most asked What is OPcache and how should it be configured in production?

    OPcache stores compiled opcodes in shared memory so PHP doesn't re-parse scripts on every request. It's the single most important PHP performance setting.

  6. Most asked What does the nullsafe operator `?->` do?

    ?-> short-circuits a method or property chain and returns null as soon as the left-hand side is null, instead of throwing an error.

  7. Most asked What changed in PHP 8 error handling for internal functions?

    Many internal functions now throw TypeError or ValueError instead of emitting warnings and returning null/false, and many notices were promoted to warnings or Error exceptions.

  8. Most asked What is late static binding?

    Late static binding means static:: resolves to the class that was actually called at runtime, whereas self:: always resolves to the class where the method is written.

  9. Most asked What is dependency injection and how does a DI container help?

    Dependency injection means a class receives its collaborators from outside (usually via the constructor) instead of creating them. A container (PSR-11) builds and wires those object graphs…

  10. Most asked What does `declare(strict_types=1)` do?

    It makes scalar type declarations strict for calls made from that file, so passing '5' to an int parameter throws TypeError instead of being coerced.

  11. Most asked What is the N+1 query problem and how do you fix it in PHP ORMs?

    N+1 happens when you load N records and then lazily query a relation for each one, producing N extra queries. Fix it with eager loading (with() in Eloquent, JOIN FETCH/addSelect in Doctrine) or…

  12. What are intersection types (PHP 8.1) and when are they useful?

    Intersection types (A&B) require a value to satisfy all listed class/interface types at once, which is useful when a dependency must implement several small interfaces.

  13. What are attributes in PHP 8 and how are they read?

    Attributes (#[Name(args)]) are structured, native metadata attached to classes, methods, properties, parameters, functions and constants, read at runtime via the Reflection API.

  14. What is the `never` return type?

    never (PHP 8.1) declares that a function never returns normally; it must either throw an exception or terminate the script with exit.

  15. What is first-class callable syntax in PHP 8.1?

    Writing foo(...) or $obj->method(...) creates a Closure from any callable, with scope and static analysis preserved, replacing string or array callables.

  16. What are `str_contains`, `str_starts_with` and `str_ends_with`?

    They're PHP 8.0 string functions that return a boolean for substring, prefix and suffix checks, replacing error-prone strpos(...) !== false patterns.

  17. What is the `Stringable` interface?

    Stringable (PHP 8.0) is automatically implemented by any class that defines toString(), so you can type-hint string|Stringable for values that can be converted to strings.

  18. What did PHP 8.3 add for class constants and overriding?

    PHP 8.3 added typed class constants (const string NAME = ...), dynamic class constant fetch (Foo::{$name}), and the #[\Override] attribute to assert that a method overrides a parent or interface…

  19. What are property hooks in PHP 8.4?

    Property hooks let you attach get and set logic directly to a property declaration, replacing boilerplate getters/setters while keeping property-access syntax.

  20. What is the Random extension added in PHP 8.2?

    PHP 8.2 introduced Random\Randomizer with pluggable engines (Secure, Mt19937, PcgOneseq128XslRr64, Xoshiro256StarStar), giving object-based, testable, seedable randomness.

  21. What are Disjunctive Normal Form (DNF) types in PHP 8.2?

    DNF types let you combine union and intersection types, as long as the expression is a union of intersections, e.g. (Countable&Iterator)|null.

  22. What is the `mixed` type and when should you avoid it?

    mixed (PHP 8.0) explicitly accepts any value, including null. It's equivalent to the union of all types and is better than no type, but usually worse than a precise one.

  23. What does the `static` return type mean?

    A static return type (PHP 8.0) means the method returns an instance of the class it was called on (late static binding), which is ideal for fluent interfaces and named constructors in inheritable…

  24. What are Fibers in PHP 8.1 and how do they relate to async PHP?

    Fibers are stackful, cooperatively scheduled coroutines: a function can pause itself with Fiber::suspend() anywhere in its call stack and be resumed later. They are the primitive async libraries…

  25. What is a WeakMap and when would you use it?

    WeakMap (PHP 8.0) maps objects to values without preventing those objects from being garbage-collected; when the key object is destroyed, its entry disappears.

  26. What is the `throw` expression in PHP 8?

    Since PHP 8.0 throw is an expression rather than a statement, so it can be used in arrow functions, the null coalescing operator and ternaries.

  27. What does `$object::class` do in PHP 8?

    PHP 8.0 allows ::class on objects, returning the fully qualified class name, equivalent to get_class($object).

  28. What does PHP 8.1's `new` in initializers allow?

    You can use new ClassName(...) in parameter default values, attribute arguments, static variable initializers and global constants, which simplifies optional dependencies.

  29. What does `array_is_list()` check?

    array_is_list() (PHP 8.1) returns true if an array's keys are consecutive integers starting at 0, i.e. it would encode as a JSON array rather than an object.

  30. What is asymmetric visibility in PHP 8.4?

    Asymmetric visibility lets a property have different read and write visibility, e.g. public private(set) string $id, so it's publicly readable but only writable inside the class.

  31. What else changed in PHP 8.4 besides property hooks?

    PHP 8.4 also added asymmetric visibility, new without parentheses in chains, array_find/array_any/array_all, lazy objects, the #[\Deprecated] attribute, a new DOM HTML5 parser, and implicitly…

  32. What is the pipe operator in PHP 8.5?

    The pipe operator |> (PHP 8.5) passes the value on its left as the single argument to the callable on its right, enabling left-to-right function chaining.

  33. What is the `#[\SensitiveParameter]` attribute for?

    #[\SensitiveParameter] (PHP 8.2) redacts a parameter's value from stack traces, so passwords or API keys don't leak into logs or error pages.

  34. What is OPcache preloading (PHP 7.4+)?

    Preloading (opcache.preload) loads and links a set of PHP files into shared memory once at server start, making those classes permanently available to every request without autoloading.

  35. What are PHP superglobals and why shouldn't business code use them directly?

    Superglobals ($_GET, $_POST, $_SERVER, $_COOKIE, $_FILES, $_SESSION, $_ENV) are always-available arrays of request and environment data. Using them deep in code couples logic to global state.

  36. What are common pitfalls with `preg_*` functions?

    Common pitfalls are unescaped user input in patterns (use preg_quote), catastrophic backtracking, forgetting the u modifier for UTF-8, and ignoring errors (preg_last_error_msg()).

Was this page helpful?

Report a mistake