Re: [RFC][DISCUSSION] Constructor behaviour of internal classes

From: Date: Sun, 01 Mar 2015 19:04:20 +0000
Subject: Re: [RFC][DISCUSSION] Constructor behaviour of internal classes
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-84113@lists.php.net to get a copy of this message
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();

« previous php.internals (#84113) next »