What are some best practices for handling class instances and variables in PHP when including files?

When including files in PHP that contain class definitions or variable declarations, it's important to consider namespace collisions and redefinitions. To avoid conflicts, it's recommended to use namespaces for classes and define variables within functions or classes to encapsulate them. Additionally, using autoloading mechanisms can help manage class loading efficiently.

// File1.php
namespace MyNamespace;

class MyClass {
    // Class definition
}

// File2.php
namespace AnotherNamespace;

class AnotherClass {
    // Class definition
}

// index.php
require_once 'File1.php';
require_once 'File2.php';

use MyNamespace\MyClass;
use AnotherNamespace\AnotherClass;

$myObject = new MyClass();
$anotherObject = new AnotherClass();