Hi,
On Fri, Nov 22, 2013 at 9:30 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi all,
>
> On Fri, Nov 22, 2013 at 10:19 AM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
>
> > On Fri, Nov 22, 2013 at 10:09 AM, Yasuo Ohgaki <yohgaki@ohgaki.net>
> wrote:
> >
> >> Anyway, constant variables are useful for VM/op code optimization.
> >>
> >> include('foo.php'); // const a = 123;
> >> const b = a;
> >>
> >> is allowed currently. We may restrict this behavior only to the same
> file,
> >> so that VM/op code optimization can be done.
> >>
> >> Currently, no expression is allowed with 'const'. Defining 'const' using
> >> another constant with the same value is useless. It's a bad coding
> >> practice
> >> in many case, I suppose.
> >>
> >> I understand use cases of scalar expressions with constants, but I'm
> >> concerned
> >> loosing door for further optimizations. (e.g. $x = $x + 123 is faster
> >> than $x = $x + a,
> >> where a is 123 in constant var hash)
> >>
> >> We may be better to restrict expressions with constants only to the same
> >> file
> >> even if we loose some benefits/flexibility in user code.
> >>
> >> Is this discussed?
> >>
> >
> > I voted 'yes', but I change my mind. Sorry.
> > If the RFC leaves door for constant optimization, I'll vote 'yes'.
> > Constant look up is not a simple hash look up.
> >
>
> One more additional note why I decided to vote 'no' for this.
> We already have dynamic constant via define().
>
> <?php
> define('A', 123);
> define('B', A*2);
>
> echo B; // 246
> ?>
>
> We can even use function return values with define().
>
> <?php
> function foo() {
> return 123;
> }
>
> define('A', foo());
>
> echo A; // 123
> ?>
>
Yes, of course that's all possible, because define() is just a function and
therefore the normal assignment rules apply. However, you didn't mention
the biggest drawback to such an approach, which is that it creates *global*
constants; there's no way to namespace them.
> I prefer 'const' to be a static for better performance, since
> we have dynamic constant by define() already.
>
Perhaps we'd have to consider how much performance is impacted on existing
code, e.g.
class A
{
const B = 123;
}
How much slower would that become if this RFC is accepted, cached or
otherwise?
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
--
--
Tjerk