Can I use an abstract class instead of a private __construct() when creating a singleton in PHP?

Posted by Pheter on Stack Overflow See other posts from Stack Overflow or by Pheter
Published on 2010-03-28T13:36:26Z Indexed on 2010/03/28 13:43 UTC
Read the original article Hit count: 185

When creating a Singleton in PHP, I ensure that it cannot be instantiated by doing the following:

class Singleton {

    private function __construct() {}
    private function __clone() {}

    public static function getInstance() {}
}

However, I realised that defining a class as 'abstract' means that it cannot be instantiated. So is there anything wrong with doing the following instead:

abstract class Singleton {

    public static function getInstance() {}
}

The second scenario allows me to write fewer lines of code which would be nice. (Not that it actually makes much of a difference.)

© Stack Overflow or respective owner

Related posts about php

Related posts about singleton