How can the PHP code snippet provided be optimized to prevent the error "Fatal error: Call to a member function alterDatabase() on a non-object"?

The error "Fatal error: Call to a member function alterDatabase() on a non-object" occurs when trying to call a method on an object that is not instantiated properly. To prevent this error, ensure that the object is properly created before calling its methods.

// Instantiate the Database class before calling the alterDatabase() method
$database = new Database();
$database->alterDatabase();