What are the potential pitfalls of instantiating a new class instance for each function call in PHP?

Creating a new class instance for each function call in PHP can lead to increased memory usage and slower performance, especially if the class constructor is complex or requires heavy resources. To solve this issue, you can consider using a singleton pattern to ensure that only one instance of the class is created and reused throughout the application.

class Singleton {
    private static $instance;

    private function __construct() {
        // Private constructor to prevent instantiation
    }

    public static function getInstance(): Singleton {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    // Add your class methods here
}

// Example of how to use the Singleton class
$singletonInstance = Singleton::getInstance();