What are the best practices for organizing PHP class files and defining namespaces to ensure proper autoloading and class resolution?

To ensure proper autoloading and class resolution in PHP, it's important to organize class files within a directory structure that reflects the namespaces they belong to. Each namespace should correspond to a directory path, and class files should follow a consistent naming convention that matches the class name. Additionally, defining namespaces at the beginning of each class file helps autoloaders locate and load classes efficiently.

// Example directory structure:
// /src
//   /Namespace1
//     MyClass1.php
//   /Namespace2
//     MyClass2.php

// MyClass1.php
namespace Namespace1;

class MyClass1 {
    // Class implementation
}

// MyClass2.php
namespace Namespace2;

class MyClass2 {
    // Class implementation
}