What are some best practices for structuring PHP code to avoid errors related to object instantiation and usage?

When working with object instantiation and usage in PHP, it's important to follow best practices to avoid errors. One common issue is not checking if an object is instantiated before using it, which can lead to "Call to a member function on null" errors. To prevent this, always check if an object is not null before calling its methods or accessing its properties.

// Incorrect way - not checking if object is instantiated
$object = new MyClass();
$object->someMethod();

// Correct way - checking if object is instantiated
$object = new MyClass();
if ($object !== null) {
    $object->someMethod();
}