🐘 PHP Beginner

What is a namespace in PHP?

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.