What is the difference between a public and private constructor in PHP?

A public constructor in PHP can be accessed and called from outside the class, while a private constructor can only be called from within the class itself. If you want to restrict the instantiation of a class to only within the class itself, you can use a private constructor.

class MyClass {
    private function __construct() {
        // Private constructor code here
    }

    public static function createInstance() {
        return new self();
    }
}

// Attempting to instantiate MyClass using new will result in a fatal error
$instance = MyClass::createInstance();