Re: [RFC] Default constructors
| From: | Andrea Faulds | Date: | Thu, 20 Nov 2014 09:38:14 +0000 |
| Subject: | Re: [RFC] Default constructors | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79028@lists.php.net to get a copy of this message | ||
> 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.
--
Andrea Faulds
http://ajf.me/