Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82749 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98389 invoked from network); 15 Feb 2015 20:20:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Feb 2015 20:20:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=bof@bof.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bof@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: bof@bof.de X-Host-Fingerprint: 80.242.145.70 mars.intermailgate.com Received: from [80.242.145.70] ([80.242.145.70:51170] helo=mars.intermailgate.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 08/42-21457-79FF0E45 for ; Sun, 15 Feb 2015 15:20:40 -0500 Received: (qmail 27554 invoked by uid 1009); 15 Feb 2015 21:20:36 +0100 Received: from 209.85.216.45 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.216.45):. Processed in 0.267021 secs); 15 Feb 2015 20:20:36 -0000 X-Antivirus-MYDOMAIN-Mail-From: bof@bof.de via mars X-Antivirus-MYDOMAIN: 1.25-st-qms (Clear:RC:1(209.85.216.45):. Processed in 0.267021 secs Process 27540) Received: from mail-qa0-f45.google.com (gmail@bof.de@209.85.216.45) by mars.intermailgate.com with RC4-SHA encrypted SMTP; 15 Feb 2015 21:20:36 +0100 Received: by mail-qa0-f45.google.com with SMTP id j7so19592691qaq.4 for ; Sun, 15 Feb 2015 12:20:34 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.140.144.82 with SMTP id 79mr422262qhq.54.1424031634856; Sun, 15 Feb 2015 12:20:34 -0800 (PST) Received: by 10.140.104.65 with HTTP; Sun, 15 Feb 2015 12:20:34 -0800 (PST) Received: by 10.140.104.65 with HTTP; Sun, 15 Feb 2015 12:20:34 -0800 (PST) In-Reply-To: <54E0FBE0.10301@gmail.com> References: <54E0FBE0.10301@gmail.com> Date: Sun, 15 Feb 2015 21:20:34 +0100 Message-ID: To: Rowan Collins Cc: internals Content-Type: multipart/alternative; boundary=001a11353d12bea49c050f263332 Subject: Re: [PHP-DEV] A modest proposal: __contructStatic From: bof@bof.de (Patrick Schaaf) --001a11353d12bea49c050f263332 Content-Type: text/plain; charset=UTF-8 Am 15.02.2015 21:05 schrieb "Rowan Collins" : > > This sounds to me like you should just be using the Singleton pattern, Of course this is singleton under the hood. > // Now wherever in the code you want the default instance, just use this: > $value = MyRedis::singleton()->get($key); You can surely see how this is more readable / easier to write: $value = MyRedir::get($key); > The nice thing about an explicit Singleton is you can migrate to Dependency Injection (call "$redis = MyRedis::singleton()" and start passing the instance around, I _can_ do that with my redis class, where I need it, by calling a method MyRedis::link() which just returns $this. But the places where I need that are rare. > ... or to a cleverer factory / service locator (store more than one instance in different static variables or a keyed array, to connect to different stores for different purposes). This i do with subclasses, where the subclass define suitable static protected properties that guide the connect logic (which sits in the baseclass). I have a global redis, a local redis (local to the host the code runs on), a volatile redis (global, but does not make prersistent dumps), and so on. Calling code ready red_global::this(), red_volatile::that(), and so on. You don't get it better readable (and greppable) as that. > Making static calls implicitly access a singleton seems like it would make code harder to read, and harder to extend, I don't understand. It's the best to read to me. And the best to extend - I just make another subclass. Of course, there are places (in other classes) where stuff needs to use such a redis instance without caring which one it is - i.e. an instances is passed to a constructor there - in which case I can use the MyRedis::link() method to get at the singleton instance. But in most (I'd guess 90%) of the calling places, it's just the static calls I need, and writing each of them as MyRedis::singleton()->xxx() is more tedious. best regards Patrick --001a11353d12bea49c050f263332--