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

From: Date: Sun, 01 Mar 2015 19:28:44 +0000
Subject: Re: [RFC][DISCUSSION] Constructor behaviour of internal classes
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-84114@lists.php.net to get a copy of this message
Am 01.03.2015 um 20:04 schrieb Dan Ackroyd:
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();
OK you are right but it's ugly code and the not callable constructor needs to be documented as it's not visible without calling. The following would be the standard way to go: final class PDORow {
    private function __construct() {
        // ...
    }
    public static function create() {
        return new self();
    }
} Marc

« previous php.internals (#84114) next »