php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60098 Static constructors, or static intializers
Submitted: 2011-10-19 17:48 UTC Modified: 2018-08-20 13:25 UTC
Votes:8
Avg. Score:4.2 ± 0.7
Reproduced:6 of 7 (85.7%)
Same Version:3 (50.0%)
Same OS:4 (66.7%)
From: syntaqx at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: * OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-10-19 17:48 UTC] syntaqx at gmail dot com
Description:
------------
I've noticed a fairly large trend in a lot of php frameworks, as well as in my 
own code, and I was curious about whether this is planned, the reasons as to why 
it might not be, or if it has even been brought up.

I've tried to find any other requests about this, but haven't been very 
successful.

Basically, my request is this:
When a class comes into existence (whether the code is in the file you're 
currently in, or you're including it), a static constructor (a common method for 
it is "::init") is called. This is called only once, the first time the class 
exists, and would act as a protected method (allowing parent-child objects to 
call it incase of a class reset?).

This would be pretty awesome, but I don't know if it's practical, or what all 
your thoughts might have been as I'm sure plenty of you have seen it floating 
around.

Thanks a bunch for taking the time to read my request, I'm excited to hear what 
you think :)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-24 16:30 UTC] dagguh at gmail dot com
You mean something like Static Initialization Blocks from Java? Yeah, I'd like to 
see them too.

http://download.oracle.com/javase/tutorial/java/javaOO/initial.html
 [2011-10-27 14:56 UTC] syntaq at gmail dot com
That seems to be what I'm requesting. Does that seem like something that could be 
implemented into PHP fairly easily?
 [2013-05-03 19:31 UTC] mkutschker-php at yahoo dot com
I think that a magic method named __init() would be a PHP way to go.
 [2015-05-27 22:35 UTC] cmb@php.net
-PHP Version: 5.4.0beta1 +PHP Version: *
 [2015-05-27 22:35 UTC] cmb@php.net
FWIW, there is a respective RFC in draft state:
<https://wiki.php.net/rfc/static_class_constructor>.
 [2018-08-20 13:25 UTC] cmb@php.net
-Status: Open +Status: Suspended -Package: SPL related +Package: Scripting Engine problem
 [2018-08-20 13:25 UTC] cmb@php.net
It seems that the “Static Class Constructor” RFC is abandoned, but
this feature request certainly needs the RFC process[1].  If
anybody is still interested in having this feature, please proceed
with this or another RFC.  For the time being, I'm suspending this
ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Aug 26 00:01:26 2019 UTC