Skip to content
AgileLogics
5 min read

Namespaces & Autoloading

Prerequisites: Exceptions & error handling

A namespace groups related classes and avoids name collisions between libraries — PHP's equivalent of a Java package.

namespace App\Models;

class User { }
use App\Models\User;

$user = new User();

Autoloading (via Composer, next lesson) means classes no longer need a manual require for every file — referencing App\Models\User for the first time triggers PHP to locate and load the right file automatically, based on a mapping Composer generates.