Re: [RFC] Static Constructor

From: Date: Thu, 20 Jun 2024 18:38:57 +0000
Subject: Re: [RFC] Static Constructor
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-123707@lists.php.net to get a copy of this message
> On Jun 20, 2024, at 9:01 AM, Erick de Azevedo Lima <ericklima.comp@gmail.com> wrot > Mike again: > > > Consider that some uses for a static function need to always occur no matter > > whether or not any other method of the class is called. My previous email [1] covered > > several. > > Here [2] is a discussion on StackOverflow of "hooks" that should be eagerly > > loaded. > Yes, I considered this approach before. But then I realized that for covering the properties > initialization, > this already existing lazy loading mechanism would suffice. At the end of the RFC, > I consider future scope to create an early-init. In my opinion, as it covers a more general use > for this init, > it should be implemented maybe using another magic method, like "__load" or > "__link", > which would be hooks on class loading process and not specific to static variables > initialization Well, that sure is depressing to hear. Looking back on all my uses for static methods, my primary use-case when building WordPress websites was to wire up hooks thus what excited me most about the prospect of getting "static constructors" in PHP was for wiring up said hooks. I get the lack of desire to support a lazy keyword, especially giving the lazy loading thread happening on the list, and I agree that lazy loading is better for the use-cases that motivated you, but it is depressing because the likelihood of someone else after so many years would have the passion and motivation to propose a __load method AND to have the skills to implement it is slim-to-none IMO. So IOW, a __load method it's almost certainly never going to happen. Such is life I guess. Well at least let me submit a parting bikeshed on the name. Rather than any form of "construct" can you at least name it __init or __initialize? It is not construction, and even in your comment where you wrote unprompted "...not specific to static variables initialization" you christened it to be "initialization." P.S. I do think lazy-loading without an explicit keyword will be counter-intuitive to many, and will result in many a StackOverflow question asking "Why are my __staticConstruct (or __init as proposed) methods not being called?!?" So if it were me I would implement them w/o default lazy loading so there would be motivation in the future to address it another way. That, or go ahead and also propose a __load method as part of this RFC. > On Jun 20, 2024, at 4:57 AM, Lynn <kjarli@gmail.com> wrote: > > Seeing an extra keyword made me think of: > ```php > class Foo > { > public constructor __construct() {} > public destructor __destruct() {} > public static constructor __construct() {} > public static destructor __destruct() {} // probably doesn't make sense right now > } > ``` > This syntax would allow named constructors in the future, or even omitting the default name. > ```php > class Foo > { > public constructor() {} > public destructor() {} > > public constructor create(int $id) > { > $this->id = $id; // no need to call the default __construct, nor needs to return > } > } > ``` > Not sure if this is even a desired path to take, just wanted to throw the idea out there. I do like the constructor/destructor vs. function naming, but as I wrote above I think "__init" — and for your approach Lynn: "initializer" — would be more applicable for static classes methods, with or without an explicit prefixed static keyword. #fwiw -Mike

« previous php.internals (#123707) next »