What is a namespace in PHP?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid PHP basics — a prerequisite for any developer role.
Answer
Namespaces solve the problem of name collisions between classes, functions, and constants in large projects or when using third-party libraries. Declare a namespace at the top of a file: namespace App\Controllers;. Access a class in another namespace with its fully qualified name: new \Vendor\Package\MyClass(), or import it with use Vendor\Package\MyClass;. You can alias imports: use Vendor\Package\MyClass as VendorClass;. PHP namespaces follow the folder structure convention (enforced by PSR-4 autoloading). All modern PHP frameworks and libraries use namespaces extensively, and Composer's autoloader maps namespaces to file paths automatically.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a PHP codebase.