Skip to content
Justice Tech School

Namespaces and Autoloading

Namespaces avoid name clashes and use imports them. Composer autoloads classes following PSR-4, so you rarely write require by hand.

Example
namespace App\Models;
class User { public function hi() { return "hi from " . __NAMESPACE__; } }
echo (new User)->hi(), "\n";
echo User::class, "\n";

Result

hi from App\Models
App\Models\User

Exercise

Import a class.

 App\Models\User;

Quiz Test

What does Composer autoloading save?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.