Re: [RFC] Default constructors
| From: | Rowan Collins | Date: | Thu, 20 Nov 2014 09:46:06 +0000 |
| Subject: | Re: [RFC] Default constructors | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79030@lists.php.net to get a copy of this message | ||
Andrea Faulds wrote on 20/11/2014 09:38:
Yes, I sort of tried to imply that with this line, but it's not really the clearest example: ++$a; // 1 The key is to realise that this isn't anything special about the operator, since these are also valid statements: $a; 1; HELLO_WORLD; The last one can actually demonstrate side-effects - if it's an undefined constant, a Notice is raised, even though the assumed string value is immediately discarded. http://3v4l.org/roLX3 -- Rowan Collins [IMSoP]On 20 Nov 2014, at 09:34, Rowan Collins <rowan.collins@gmail.com> wrote: Note that there are many, many places where expressions are actioned for their side effects even though the result is discarded. The following will increment $a 3 times, even though it is discarded each time: $a = 0; ++$a; // 1 class Foo { function __construct() {} } new Foo(++$a); // 2 function bar() {} bar(++$a); // 3 class WTF {} new WTF(++$a); // suddenly, PHP becomes lazy Test: http://3v4l.org/tcnv63 (note that HHVM increments all 4 times; PHP 4 only twice because class Foo has no old-style constructor) Lazy evaluation certainly has benefits, but it's not, in general, a feature of PHP. That it happens in this one very specific case is what is so unexpected.You’re forgetting perhaps the most common and fundamental case in PHP (and C, from which it comes): expression statements. Any expression, followed by a semicolon, is a valid statement. The expression’s result is discarded, but it’s always evaluated. Otherwise function calls wouldn’t work, nor would assignment, both of which are expression statements.