Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82545 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6847 invoked from network); 12 Feb 2015 16:49:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Feb 2015 16:49:50 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.208 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.208 imap2-3.ox.privateemail.com Received: from [192.64.116.208] ([192.64.116.208:59903] helo=imap2-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A0/0A-50473-BA9DCD45 for ; Thu, 12 Feb 2015 11:49:49 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id F1FA98C007D; Thu, 12 Feb 2015 11:49:43 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap2.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap2.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 3i8DVhKazR-R; Thu, 12 Feb 2015 11:49:43 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id D61538C0069; Thu, 12 Feb 2015 11:49:42 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) In-Reply-To: <007b01d046df$1bde01d0$539a0570$@tutteli.ch> Date: Thu, 12 Feb 2015 16:49:39 +0000 Cc: Nikita Nefedov , =?utf-8?Q?Pavel_Kou=C5=99il?= , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <13D542A4-16A3-40AF-8982-E851AF7AD9D3@ajf.me> References: <8703B53E-2C4A-4AC6-95C4-D4F19C6D5221@ajf.me> <54DAF884.7000508@lerdorf.com> <203e611c8e0b03568a868b8d931aec37@mail.gmail.com> <61E01A7C-C5C8-447F-A8FA-E12A18E847CA@ajf.me> <85D76C91-9A9E-459B-847A-619AA21B9262@ajf.me> <342635C4-F0EA-4557-A37F-753E51100402@ajf.me> <007b01d046df$1bde01d0$539a0570$@tutteli.ch> To: Robert Stoll X-Mailer: Apple Mail (2.2070.6) Subject: Re: [PHP-DEV] [VOTE] Scalar Type Hints From: ajf@ajf.me (Andrea Faulds) Hey Robert, > On 12 Feb 2015, at 16:15, Robert Stoll wrote: >=20 > There are several programming languages which do support dynamic = typing and method overloading somehow (Clojure, Erlang, Prolog, Cecil = and certainly more). Most of them use a multi-method approach and I = think if PHP would introduce function/method overloading, then such an = approach would be appropriate. Interesting, I should maybe look into those. I=E2=80=99m not sure if all = of those are really=E2=80=A6 =E2=80=9Cdynamic=E2=80=9D as such. Are you = sure the overloading you=E2=80=99re referring to there isn=E2=80=99t = pattern-matching? > Right now, I need to implement the dynamic dispatching quasi myself = which might be more verbose but is also more cumbersome (and uglier = IMO). Consider the following (fictional example): >=20 > I want to write a Logger-Service which provides one public method = "log" which writes all kind of objects to a log. The corresponding = classes do not all belong to my code base, are part of third party = libraries respectively, so I am not able to introduce some interface = which all classes implement. The strategy pattern is certainly a good = idea for this problem but nevertheless, somewhere I need to have the = distinction based on many if/else with instanceof (latest in the = LoggerStrategyFactory) -- off topic, if someone has a better design = approach to handle this problem, then let me know it in a private = message, would be interesting as well ;) >=20 > class Logger{ > public function log($x){ > if($x instanceof Foo){ > logFoo($x); > }else if($x instanceof Bar){ > logBar($x); > } > //... > }else{ > throw new Exception("type ".gettype($x)." not supported"); > } > } > private function logFoo(Foo $f){ > //.. > } > private function logBar(Bar $b){ > //.. > } > //... > } >=20 > With method overloading I could write the following, removing the = hassle to write the dynamic dispatch myself: >=20 > class Logger{ > public log(){ > $this->_log($x); > } > private function _log(Foo $f){ > //.. > } > private function _log(Bar $b){ > //.. > } > //... > private function _log($x){ > throw new Exception("type ".gettype($x)." not supported"); > } > } >=20 > Which is cleaner IMO. We could also add some sort of pattern-matching syntax which could do = the same thing, but would have usefulness beyond merely dispatching to = multiple methods. -- Andrea Faulds http://ajf.me/