Questions Every Good PHP Developer Should Be Able to Answer
Inspired by similar discussions for other ecosystems, there is a specific set of architectural and behavioral questions that distinguish a competent PHP practitioner from a novice scripter. These aren't just trivia; they probe your understanding of how the language interacts with the operating system, manages memory, and scales under load. A solid grasp of these concepts prevents fragile applications that collapse unpredictably in production.
Understanding Autoloading and Namespaces
One fundamental question is: How does PHP resolve class names without explicit include statements?
While many developers start with the legacy spl_autoload_register or simple include paths, a modern answer must involve PSR-4 autoloading. This standard defines a directory structure where the namespace hierarchy maps directly to folder depth. For example, a class App\User\Models\Profile belongs in app/User/Models/Profile.php.
// composer.json autoload configuration
"psr-4": {
"App\\": "src/"
}
When you call new User(), the autoloader takes the fully qualified class name, strips the namespace prefix, and maps the remaining segments to a file path. If the file isn't found, it throws a ErrorException (in PHP 8.1+) or a ClassNotFoundException. A good developer knows that this mechanism relies on the existence of both the class definition and the corresponding entry in composer.json, which is then run via composer dump-autoload to generate the optimized map file. Relying solely on require_once for every dependency creates tight coupling and makes unit testing significantly harder.
Memory Management and Garbage Collection
Another critical area is memory behavior. The question often arises: How does PHP clean up resources, and when does it actually run?
PHP uses reference counting combined with a cyclic garbage collector (GC). Reference counting means that if a variable holding an object goes out of scope or loses its last reference, the memory is freed immediately. However, circular referencesâwhere Object A holds a reference to Object B, and Object B holds back to Object Aâprevent immediate cleanup because the reference count never hits zero.
The cyclic GC runs automatically based on a threshold but can be forced manually using gc_collect_cycles(). A pitfall occurs when developers use static variables or global references in loops without realizing they might hold onto large data structures longer than intended. Understanding that unset() does not always trigger immediate memory reclamation is vital for preventing memory exhaustion errors (Fatal error: Allowed memory size exhausted) in long-running CLI scripts or single-process web servers like PHP-FPM.
Error Handling and Exception Hierarchy
How should errors be handled, and what is the difference between exceptions and errors?
In modern PHP (versions 7.4+), many fatal runtime issues are thrown as ErrorException objects. This allows them to be caught in a standard try-catch block if the error level is set appropriately. A good developer knows how to use set_error_handler() to log exceptions rather than displaying raw HTML stack traces to users.
set_exception_handler(function ($e) {
// Log to file, send 500 response, don't expose details
error_log($e->getMessage());
});
Understanding the difference between throw new \Exception() and triggering an error via trigger_error() is also important. Exceptions stop execution immediately unless caught, whereas errors can be silenced or logged depending on configuration. Ignoring this distinction leads to unpredictable application flow when handling invalid input or database connection failures.
SAPIs and Execution Context
Finally, a senior developer must distinguish between different Server API (SAPI) environments. PHP-FPM is designed for multi-process architectures where requests are handled by child processes spawned by a master process. This introduces statelessness requirements; sessions and caches must not be stored in memory within the worker if they need to survive restarts.
Conversely, CLI interfaces handle scripts differently, often allowing file handles to persist across calls in ways web contexts do not. Knowing whether your code is running under Apache (mod_php), Nginx (via FPM), or CLI dictates how you manage resources like database connections and temporary files. Misconfiguring the SAPI can lead to silent failures where data is processed but never persisted, or where file locks are held indefinitely by a web server expecting a different lifecycle than the CLI tool expects.
