On 1 March 2015 at 18:35, Marc Bennewitz <dev@mabe.berlin> wrote:
> What are the reasons to not make this class instantiable / extendable ?

That class handles database state / resources that are not exposed
through the userland classes. It's correct in my opinion for it to not
be extendible or instantiable. Obviously it would be awesome if they
were covered by an interface, to allow testign to be easier, but
that's a separate issue.

> That's definitely better but it's an behavior not possible to implemented in
> user land code as it would be impossible to get such an object.

The same behaviour can seen with the code below.

cheers
Dan

class Foo {

    private static $internalConstruction = false;

    public function __construct() {
        $constructionAllowed = !self::$internalConstruction;
        self::$internalConstruction = false;

        if ($constructionAllowed == false) {
            throw new \Exception("External construction not allowed");
        }
    }

    public static function create() {
        self::$internalConstruction = true;
        return new self();
    }
}

$foo = Foo::create();
var_dump($foo);
$bar = new Foo();

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to