Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82741 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81916 invoked from network); 15 Feb 2015 19:31:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Feb 2015 19:31:02 -0000 Authentication-Results: pb1.pair.com header.from=ua.san.alex@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ua.san.alex@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.169 as permitted sender) X-PHP-List-Original-Sender: ua.san.alex@gmail.com X-Host-Fingerprint: 209.85.213.169 mail-ig0-f169.google.com Received: from [209.85.213.169] ([209.85.213.169:54787] helo=mail-ig0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0B/EB-06835-5F3F0E45 for ; Sun, 15 Feb 2015 14:31:02 -0500 Received: by mail-ig0-f169.google.com with SMTP id hl2so28478460igb.0 for ; Sun, 15 Feb 2015 11:30:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=DcCe7Khgcl5P+IgpTBK5lyDC1fu0MiEkdiJKS7cGfPQ=; b=MJGwaZjVySIgFmHOldEF6bSQT2370p6ckpR7z/X/gbgr/wtW94KMVk1pPIcCkMvbeX Dg0FqTSuOzPFcYvj1oC1Rh+Gw0Cg9E+NJZP+LLOoBpQ7JFOcJLufBGbtO6hVbfKsCsQG zZh4V5TNCMNXZyvJW32LwEbgKwR+Dohr5P72GHZ7xciFhwPXc//VaToQywkIIiWZIaxI IJGXmTKeT9DLA2j5/hNn8KdT/vqQZd5oCR+gcCuhXzQerr5HQvVkpoNURSemuWG0JuIi dCyMeAoVVy4BxavdiWE20hYq4fJieNzdrMbaOL/IZAMZkg81g1OmuSaYubOiRRBwx2Zr f0lQ== MIME-Version: 1.0 X-Received: by 10.43.111.66 with SMTP id en2mr25809907icc.6.1424028659238; Sun, 15 Feb 2015 11:30:59 -0800 (PST) Received: by 10.50.93.3 with HTTP; Sun, 15 Feb 2015 11:30:59 -0800 (PST) In-Reply-To: References: Date: Sun, 15 Feb 2015 21:30:59 +0200 Message-ID: To: Patrick Schaaf Cc: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] A modest proposal: __contructStatic From: ua.san.alex@gmail.com ("S.A.N") 2015-02-15 21:04 GMT+02:00 Patrick Schaaf : > Hello Internals, > > seeing the static calling of instance methods being discussed again, I want > to ask whether the following idea would maybe have a chance? > > In our codebase we have one set of classes where it was very useful to be > able to call the same methods both statically and nonstatically: a set of > wrapper classes over phpredis where subclasses "know" which set of redis > servers to talk to (plus other config like retry strategies and timeouts). > In most cases calling code does not need to be concerned with these details > and just calls methods like red_global::get() or red_local::put(). > > By neccessity the implementation of this class set, must make use of both > __call() and __callStatic() magic methods, with both then dispatching to a > delegate phpredis instance, and in the case of __callStatic(), making > up-front a singleton like "new self" once. For a set of additional helper > methods (not present on the underlying phpredis) I additionally have a > special mapping array of real method names to internal implementation names. > > A similar approach can be useful for database connection abstraction > classes. > > Now for the idea how this might be made much more simple: onviously in the > light of the "static call to method using $this" warning/detection, PHP > already knows exactly where it hits such a static call to an instance > method. At that point, check whether the class has a "static instance" > already. When it has one, call the method with $this being that "static > instance". Otherwise, create the static instance, calling a new magick > method named __constructStatic() to initialize it. Only classes that have > such a __constructStatic() method, would be treated that way. For other > classes the "static call to instance method" case would be an error as > before/discussed. > > This approach would clearly simplify my redis wrapper classes, maybe even > enable them to be proper subclasses of phpredis (saving an indirection to a > $this->delegate object). All the special dispatch and __callStatic > instantiation logic could go away. > > The cost would be one internal pointer (to the "static instance" object) > per class, a kind of hidden "static protected" property, and only needed > when the class (or one of its superclasses) _has_ a __constructStatic > method in the first place. > > What do you all think? Interesting? Somebody hot for helping with > impementation? Then I'd make an RFC of it. > > best regards > Patrick +1