Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82737 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75084 invoked from network); 15 Feb 2015 19:04:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Feb 2015 19:04:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@bof.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@bof.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain bof.de designates 80.242.145.70 as permitted sender) X-PHP-List-Original-Sender: php@bof.de X-Host-Fingerprint: 80.242.145.70 mars.intermailgate.com Received: from [80.242.145.70] ([80.242.145.70:49292] helo=mars.intermailgate.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/8A-06835-EADE0E45 for ; Sun, 15 Feb 2015 14:04:15 -0500 Received: (qmail 5617 invoked by uid 1009); 15 Feb 2015 20:04:12 +0100 Received: from 209.85.192.52 by mars (envelope-from , uid 89) with qmail-scanner-1.25-st-qms (clamdscan: 0.96.2/20063. spamassassin: 3.3.1. perlscan: 1.25-st-qms. Clear:RC:1(209.85.192.52):. Processed in 0.280577 secs); 15 Feb 2015 19:04:12 -0000 X-Antivirus-MYDOMAIN-Mail-From: php@bof.de via mars X-Antivirus-MYDOMAIN: 1.25-st-qms (Clear:RC:1(209.85.192.52):. Processed in 0.280577 secs Process 5610) Received: from mail-qg0-f52.google.com (gmail@bof.de@209.85.192.52) by mars.intermailgate.com with RC4-SHA encrypted SMTP; 15 Feb 2015 20:04:11 +0100 Received: by mail-qg0-f52.google.com with SMTP id h3so20546307qgf.11 for ; Sun, 15 Feb 2015 11:04:09 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.140.21.48 with SMTP id 45mr317638qgk.87.1424027049791; Sun, 15 Feb 2015 11:04:09 -0800 (PST) Received: by 10.140.104.65 with HTTP; Sun, 15 Feb 2015 11:04:09 -0800 (PST) Received: by 10.140.104.65 with HTTP; Sun, 15 Feb 2015 11:04:09 -0800 (PST) Date: Sun, 15 Feb 2015 20:04:09 +0100 Message-ID: To: internals Content-Type: multipart/alternative; boundary=001a11c129947416d4050f252235 Subject: A modest proposal: __contructStatic From: php@bof.de (Patrick Schaaf) --001a11c129947416d4050f252235 Content-Type: text/plain; charset=UTF-8 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 --001a11c129947416d4050f252235--