Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49438 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37452 invoked from network); 17 Aug 2010 13:15:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Aug 2010 13:15:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.zuelke@bitextender.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=david.zuelke@bitextender.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain bitextender.com from 80.237.132.12 cause and error) X-PHP-List-Original-Sender: david.zuelke@bitextender.com X-Host-Fingerprint: 80.237.132.12 wp005.webpack.hosteurope.de Received: from [80.237.132.12] ([80.237.132.12:39579] helo=wp005.webpack.hosteurope.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/54-08806-36B8A6C4 for ; Tue, 17 Aug 2010 09:15:17 -0400 Received: from [78.35.9.21] (helo=[172.16.1.171]); authenticated by wp005.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) id 1OlM0X-0003Ur-10; Tue, 17 Aug 2010 15:15:13 +0200 Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: multipart/signed; boundary=Apple-Mail-16-948518513; protocol="application/pkcs7-signature"; micalg=sha1 In-Reply-To: Date: Tue, 17 Aug 2010 15:15:12 +0200 Cc: RQuadling@googlemail.com, Jingcheng Zhang , php-dev Message-ID: <681CB0AD-6391-4A28-82C7-3A056CA2F714@bitextender.com> References: To: Ferenc Kovacs X-Mailer: Apple Mail (2.1081) X-bounce-key: webpack.hosteurope.de;david.zuelke@bitextender.com;1282050917;61b2d08b; Subject: Re: [PHP-DEV] Static initialization block support in Class? From: david.zuelke@bitextender.com (=?iso-8859-1?Q?David_Z=FClke?=) --Apple-Mail-16-948518513 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 17.08.2010, at 10:46, Ferenc Kovacs wrote: > Tue, Aug 17, 2010 at 10:04 AM, Richard Quadling = wrote: >=20 >> On 17 August 2010 08:39, Jingcheng Zhang wrote: >>> Hello internals, >>>=20 >>> I wonder whether it is possible to implement "static initialization >> block" >>> feature in PHP, for example: >>>=20 >>> >> class Foo { >>>=20 >>> } >>> class Bar { >>> public static $baz =3D 'baz'; >>> public static $foo; >>> static { >>> // After loading this file, self::$foo is initialized as a Foo >>> instance. >>> self::$foo =3D new Foo(); >>> } >>> } >>> ?> >>>=20 >>> Currently we have to do this outside the class definition as static >> variable >>> initialization is only limited to constant values. >>> However in some circumstance, "dynamic" initialization of static = variable >> is >>> expected and meaningful. >>>=20 >>> Thanks in advance! >>>=20 >>> -- >>> Best regards, >>> Jingcheng Zhang >>> P.R.China >>>=20 >>=20 >> Implementing a singleton method is a common solution and one that is >> well understood and documented. >>=20 >> Another option would be to put the initialisation immediately after >> the class definition, so once the class is loaded, a static instance >> is prepared. This saves the consumer of the class from having to do 1 >> additional line of code. >>=20 >> Or you could load the public static methods with a JIT call to = prepare >> the static instance. Overhead is that every call will have to test >> self::$instance >>=20 >>=20 >> There is probably some argument against "statics" being "dynamic" >> though ... not my area of expertise to argue either way. >>=20 >> -- >> Richard Quadling. >>=20 >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >>=20 >>=20 > This was brought up in the past: > http://www.mail-archive.com/internals@lists.php.net/msg46458.html > I still think that it's weird, that I can define a constant to a = dynamic > value (eg. by a complex expression or a function's return value), but = I > can't do that with the class consts. and with 5.3, we have two = different > kind of consts, you can define constants in compile time with const = where > you can't use expressions, and you can use the define method, where = you can > use expressions. > and you can combine them: >=20 > define("NOW", time()); > var_dump(NOW); > const BAR =3D NOW; > var_dump(BAR); > class baz{ > const BAR =3D NOW; > } > var_dump(baz::BAR);' >=20 > with that in mind, I think we could allow complex expression to the = const: > the expression will be stored as-is, and when it's referenced (JIT) = then it > will be evaluated. >=20 > and this could be used also for variables also: >=20 > class foo{ > public $now =3D time(); > } > $foo =3D new foo; > echo $foo->now; >=20 > ps: I predict somebody will say: can of worms! :) >=20 > Tyrael Can do: can of worms. To the original poster: use a singleton or add the init declarations = after the class declaration, works just fine with autoloads (I recommend = the singleton approach). - David --Apple-Mail-16-948518513 Content-Disposition: attachment; filename=smime.p7s Content-Type: application/pkcs7-signature; name=smime.p7s Content-Transfer-Encoding: base64 MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIKJjCCBMww ggQ1oAMCAQICEByunWua9OYvIoqj2nRhbB4wDQYJKoZIhvcNAQEFBQAwXzELMAkGA1UEBhMCVVMx FzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMTcwNQYDVQQLEy5DbGFzcyAxIFB1YmxpYyBQcmltYXJ5 IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA1MTAyODAwMDAwMFoXDTE1MTAyNzIzNTk1OVow gd0xCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5WZXJpU2lnbiwgSW5jLjEfMB0GA1UECxMWVmVyaVNp Z24gVHJ1c3QgTmV0d29yazE7MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZl cmlzaWduLmNvbS9ycGEgKGMpMDUxHjAcBgNVBAsTFVBlcnNvbmEgTm90IFZhbGlkYXRlZDE3MDUG A1UEAxMuVmVyaVNpZ24gQ2xhc3MgMSBJbmRpdmlkdWFsIFN1YnNjcmliZXIgQ0EgLSBHMjCCASIw DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMnfrOfq+PgDFMQAktXBfjbCPO98chXLwKuMPRyV zm8eECw/AO2XJua2x+atQx0/pIdHR0w+VPhs+Mf8sZ69MHC8l7EDBeqV8a1AxUR6SwWi8mD81zpl Yu//EHuiVrvFTnAt1qIfPO2wQuhejVchrKaZ2RHp0hoHwHRHQgv8xTTq/ea6JN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