Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73014 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70765 invoked from network); 8 Mar 2014 23:01:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2014 23:01:31 -0000 Authentication-Results: pb1.pair.com smtp.mail=are.you.winning@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=are.you.winning@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.43 as permitted sender) X-PHP-List-Original-Sender: are.you.winning@gmail.com X-Host-Fingerprint: 209.85.216.43 mail-qa0-f43.google.com Received: from [209.85.216.43] ([209.85.216.43:47176] helo=mail-qa0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 29/F3-44862-A41AB135 for ; Sat, 08 Mar 2014 18:01:31 -0500 Received: by mail-qa0-f43.google.com with SMTP id j15so5500109qaq.2 for ; Sat, 08 Mar 2014 15:01:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=Ex49uW0kgx/tK/8i8S67Z0hS7uXvFZKpyzPyoTc9st0=; b=TgUzkzXIHcqReP99Vn0WZjjPNqDItEOTkKqq+PZgRswbMbduNNgBkDKkMwAZslr1Vy pgO57waG748I063F2S8vQRLaYxNyqGe6MlOWcbFdcCL/iQgr1uzrnusD9+qnmQzwmblB DLtzO17PuWu5hX4IVUKEU6owYjyvKMhLQBjGw5vH5N505gx3z7ntceGG60XAA9dQzr2M vSZePeZkwCqS7zH+brvnj7iTKcreaGc6KZM0oklWZSogd4oGSgr1dU3rjyMsT5gdbC+E 5cST6CrPElLaJD/AnLIUEMaCuhsPY/yHAytUi74aqdDSPXk5QtbIO23m+JCwTj0vldFb ag/w== MIME-Version: 1.0 X-Received: by 10.140.38.168 with SMTP id t37mr29345306qgt.33.1394319688284; Sat, 08 Mar 2014 15:01:28 -0800 (PST) Sender: are.you.winning@gmail.com Received: by 10.229.229.198 with HTTP; Sat, 8 Mar 2014 15:01:28 -0800 (PST) In-Reply-To: <29BDF40C-562D-4943-877B-70701335D3AA@ajf.me> References: <03CFE6C6-0742-4928-BD2B-E9C920E9246A@ajf.me> <29BDF40C-562D-4943-877B-70701335D3AA@ajf.me> Date: Sat, 8 Mar 2014 23:01:28 +0000 X-Google-Sender-Auth: CB6XT6PfyihmRpvrkUTQhE7yGio Message-ID: To: Andrea Faulds Cc: Marco Schuster , internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Idea: ifdef-like feature to ease userland BC From: daverandom@php.net (Chris Wright) On 8 March 2014 22:33, Andrea Faulds wrote: > > On 8 Mar 2014, at 21:48, Marco Schuster wrote: >> This breaks if you use new syntax features like the array shortcut >> $var=[];, traits or similar. > > > Right, but those aren't problems. I can't see anyone willingly writing: > > #ifdef PHP_5_4 > $a = [1,2,3]; > #else > $a = array(1,2,3); > #endif > > They will simply write: > > $a = array(1,2,3); > > For traits, again, it's unlikely people will want to write two sets of code and deliberately duplicate things. They'll simply just not use traits. > > The only case where what you're saying makes sense is where features are removed in favour of alternatives, and I can't see that happening for any syntactic features. > > -- > Andrea Faulds > http://ajf.me/ > I'm not necessarily in favour of this idea as a core feature but to give a more practical example of how this could be useful: generators can give considerable efficiency gains where previously more resources would have been consumed by a process yielding values over time. Before 5.5, generator code would cause a parse error. Imagine the following somewhat contrived example: function some_func() { $fp = fopen('some_file.ext'); $result = array(); while (false !== $line = fgets($fp)) { $result[] = $line; } return $result; } foreach (some_func() as $foo) { // do stuff } Imagine that some_func() is only ever used in the context of a foreach. For BC reasons, we have to keep this API. Since 5.5, we could use a generator (yes, we could use an iterator, I said it was contrived :-P), but we need to provide BC with older PHP versions: function some_func() { $fp = fopen('some_file.ext'); #if PHP_VERSION >= 5.5.0 while (false !== $line = fgets($fp)) { yield $line; } #else $result = array(); while (false !== $line = fgets($fp)) { $result[] = $line; } return $result; #endif } As another example, imagine a heavy data processing routine that uses goto for performance reasons, but before 5.3 would fall back to using private methods instead: #if PHP_VERSION >= 5.3.0 private function partOfRoutine() #else partOfRoutine: #endif { /* stuff */ #if PHP_VERSION >= 5.3.0 anotherPartOfRoutine(); #else goto anotherPartOfRoutine; #endif } Another advantage for performance-minded people is that a pre-processor is a one-shot deal, whereas runtime checks are potentially performed a few hundred times a second with the exact same result every time for ever more. **However** the thought occurs that, while potentially useful, a pre-processor of this nature could easily be done with a userland implementation, a composer dependency and a composer after-install/after-update script... Thanks, Chris