Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84623 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38429 invoked from network); 12 Mar 2015 09:38:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 09:38:49 -0000 X-Host-Fingerprint: 91.115.245.126 91-115-245-126.adsl.highway.telekom.at Received: from [91.115.245.126] ([91.115.245.126:10279] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3D/0D-64353-9AE51055 for ; Thu, 12 Mar 2015 04:38:49 -0500 Message-ID: <3D.0D.64353.9AE51055@pb1.pair.com> To: internals@lists.php.net Date: Thu, 12 Mar 2015 10:38:45 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 References: <6D.2C.32765.10EC0055@pb1.pair.com> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Posted-By: 91.115.245.126 Subject: Re: [PHP-DEV] static constructor From: mail@deroetzi.de (Johannes Ott) > > What about inheritance? > I think dynamic class-constructor would make much more sense. > A function which can analyse real class and do initialisation. > > class A > { > protected static function __class_construct() > { > echo get_called_class().” class is defined\n"; > } > } > > class B extends A > { > } > > #### output #### > A class is defined > B class is defined > I think class-constructor and static-constructor are two different things. I even think that a inherited class-constructor is a dangerous thing because you have huge depencies between all subclasses. For a quick and dirty example: abstract class A { protected static $value; protected static function __class_construct() { switch (get_called_class()): case 'B': self::$value = 1; break; case 'C': self::$value = 2; break; default: self::$value = 3; } } class B extends A { public static function isOne() { return self::$value == 1; } } class C extends A { public static function isTwo() { return self::$value == 2; } } That not wellformed code for three reasons: 1. Class A must know about all of it's subclasses, so you can not easily reuse this class A in other projects because you have to refactor the complete __class_constructor. 2. Huge dependcies between all subclasses. If you want to implement a class D although extending class A you have to look in each other class for sideeffects of the the changes you have todo in the __class_construct for this class D 3. As you can see in the quick example you have a depency of class loading order as well the result of B::isOne() and C::isTwo() depends in which order you have loaded your classfiles. Even worst for example You are using only class B for the beginn B::isOne() giving you true, later you load class C for another reason and suddenly B::isOne() gives you false. A static constructor however encapsulate the initialization to each class and should only initialize private members which can be accessed by protected getters by the subclass. For the example: abstract class A { private static $one; private static function __static() { self::$one = 1; } protected static function getOne() { return self::$one; } } class B extends A { public static function isOne() { return self::getOne() == 1; //Now always true } } class C extends A { public static function isTwo() { return self::getOne() == 2; //Now always false } } Regards, -- DerOetzi