# Constant and expression ?

- Thread: https://externals.io/message/48339
- Original archive: https://news-web.php.net/php.internals/48339
- Started by Frederic Hardy on 2010-05-19T08:54:03+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #48339 — Frederic Hardy — 2010-05-19T08:54:03+00:00

URL: https://externals.io/message/48339#48339

Hello !

Is there any feature request about constant and expression ?
I would like to write something like that :

<?php

namespace foo\bar\directories {
     const tmp = __DIR__ . '/tmp';
}

?>

Best regards,
Fred.

---

## #48340 — Tjerk Anne Meesters — 2010-05-19T09:03:49+00:00

URL: https://externals.io/message/48339#48340 · In reply to: #48339

AFAIK class constants are optimized for speed and therefore don't
support expressions; don't expect this to change any time soon ;-)

On Wed, May 19, 2010 at 4:54 PM, Frederic Hardy
<frederic.hardy@mageekbox.net> wrote:
> Hello !
>
> Is there any feature request about constant and expression ?
> I would like to write something like that :
>
> <?php
>
> namespace foo\bar\directories {
>    const tmp = __DIR__ . '/tmp';
> }
>
> ?>
>
> Best regards,
> Fred.
>
> --
> 
> 
>
>



-- 
--
Tjerk

---

## #48341 — Pierre Joye — 2010-05-19T09:10:50+00:00

URL: https://externals.io/message/48339#48341 · In reply to: #48340

hi,


> AFAIK class constants are optimized for speed and therefore don't
> support expressions; don't expect this to change any time soon ;-)

It would require to do a two passes compilation (or something along
this line) as you can define constants later in your script. They will
also be defined at runtime, not at buildtime. pecl's hidef may help
:).

Cheers,
-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #48343 — Tjerk Anne Meesters — 2010-05-19T10:21:27+00:00

URL: https://externals.io/message/48339#48343

On Wed, May 19, 2010 at 5:52 PM, Frederic Hardy
<frederic.hardy@mageekbox.net> wrote:
> Hello !
> > […]
>
> So the best solution is something like <?php
> define('foo\bar\directories\tmp', __DIR__ . '/tmp'); ?>...
> It 's strange that "define()" function can do something that the "const"
>  keyword can't do, even if "const" keyword evaluation is done at compilation
> time and "define()" evaluation is done at runtime.

That's why there's a cost penalty to using `define()`.

I wrote a small article that gives an idea of the speed differences:
http://shwup.blogspot.com/2010/04/about-constants.html


>
> Best regards,
> Fred.
>
> --
> ========================================================================
> Frédéric Hardy : Architecte d'application/Admin. système/Ergonome
>        Status : En recherche d'emploi
> ========================================================================
>
>



-- 
--
Tjerk

---

## #48344 — fqqdk — 2010-05-19T11:03:59+00:00

URL: https://externals.io/message/48339#48344 · In reply to: #48343

2010/5/19 Tjerk Anne Meesters <datibbaw@php.net>

> On Wed, May 19, 2010 at 5:52 PM, Frederic Hardy
> <frederic.hardy@mageekbox.net> wrote:
> > […]
> compilation
> > […]
>
> That's why there's a cost penalty to using `define()`.
>
> I wrote a small article that gives an idea of the speed differences:
> http://shwup.blogspot.com/2010/04/about-constants.html
>
>
> > […]
>
>
>
> --
> --
> Tjerk
>
> --
> 
> 
>

How about extending the usage of the 'final' keyword to support a java-like
syntax?

class Foo {
   private static final $bar = 'ex' . 'pression';
   // and also
   private final $baz = 'another'.'expression';
   // and maybe
   private final $inTheCtor;

   public function __construct($aValue) {
       $this->inTheCtor = $value;
   }
}

---

## #48345 — Richard Quadling — 2010-05-19T11:04:20+00:00

URL: https://externals.io/message/48339#48345 · In reply to: #48343

> On Wed, May 19, 2010 at 5:52 PM, Frederic Hardy
> <frederic.hardy@mageekbox.net> wrote:
> > […]
>
> That's why there's a cost penalty to using `define()`.
>
> I wrote a small article that gives an idea of the speed differences:
> http://shwup.blogspot.com/2010/04/about-constants.html
>
>
> > […]
>
>
>
> --
> --
> Tjerk

With regard to HiDef and the hidef.ini_path setting, and the comment
on [1] regarding "However, all this goodness comes with a drawback:
configuration settings are instance wide, which impacts all setups of
PHP that runs beyond a single web request (e.g. Apache module or
FastCGI).", could the ini entry be placed in a ...

[HOST=]

section in the ini file? [2]

If so, could this be the best solution?

Regards,

Richard.

[1] http://shwup.blogspot.com/2010/04/about-constants.html
[2] http://docs.php.net/manual/en/ini.sections.php

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #48347 — Christian Schneider — 2010-05-19T11:49:54+00:00

URL: https://externals.io/message/48339#48347 · In reply to: #48344

fqqdk wrote:
> How about extending the usage of the 'final' keyword to support a java-like
> syntax?
> 
> class Foo {
>    private static final $bar = 'ex' . 'pression';

In my book "final" in Java gets an award for sickest abuse of a keyword.
So no, I don't consider this an option. Simply use a class variable (or
 `define()`) if you need to store some runtime calculated value.

"But <insert other language> can do this" alone is not sufficient as a
reason to bloat PHP IMHO.

- Chris

---

## #48349 — Ferenc Kovacs — 2010-05-19T12:04:13+00:00

URL: https://externals.io/message/48339#48349 · In reply to: #48347

On Wed, May 19, 2010 at 1:49 PM, Christian Schneider
<cschneid@cschneid.com>wrote:

> fqqdk wrote:
> > […]
> java-like
> > […]
>
> In my book "final" in Java gets an award for sickest abuse of a keyword.
> So no, I don't consider this an option. Simply use a class variable (or
> `define()`) if you need to store some runtime calculated value.
>
> "But <insert other language> can do this" alone is not sufficient as a
> reason to bloat PHP IMHO.
>
> - Chris
>

I think that is pretty subjective, but I did suprised when I found out, that
the global scope constants behaves differently than the class consts.

Allowing the class consts to be initialized in runtime(if neccessary)
wouldn't breake any php application, only slow them.
Can't we do something, to allow broader usage of consts without sacrificing
performance?
So I propose to add a new syntax: literal.
literal should behave how the const behaved, and const should be initialized
in runtime.
maybe we could check from runtime, that the const was a dynamic value, or
literal, and generate a notice for the developer to change the const to
literal for gaining performance.

What do you think?

Tyrael

---

## #48352 — johannes@schlueters.de — 2010-05-19T12:43:14+00:00

URL: https://externals.io/message/48339#48352 · In reply to: #48344

Hi,


> 2010/5/19 Tjerk Anne Meesters <datibbaw@php.net>
> > […]

Unfortunately this doesn't tell, what you actually measured. And note
that as of 5.3 the "class constant" syntax can be used in the global
scope, too.

> How about extending the usage of the 'final' keyword to support a java-like
> syntax?

No. We have a const keyword. Rather somebody interested in this topic
might try to come up with a patch to at least allow "constant
expressions" this should serve most needs.

constant expressions might be stuff like __DIR__.'/foobar.php' or
CONST_A | CONST_B.

We already have two steps in the constant resolution for constructs like
these:

php > class A { const C = FOOBAR; }
php > define('FOOBAR', 42);
php > echo A::C;
42

See zval_update_constant_ex in zend_API.c as a starting point. While
such an approach would also require some parser work and some clever
idea where/how to store the expression to be evaluated etc.

johannes

---

## #48355 — Tjerk Anne Meesters — 2010-05-19T14:57:27+00:00

URL: https://externals.io/message/48339#48355 · In reply to: #48345

On Wed, May 19, 2010 at 7:04 PM, Richard Quadling
<rquadling@googlemail.com> wrote:
> 
> > […]
>
> With regard to HiDef and the hidef.ini_path setting, and the comment
> on [1] regarding "However, all this goodness comes with a drawback:
> configuration settings are instance wide, which impacts all setups of
> PHP that runs beyond a single web request (e.g. Apache module or
> FastCGI).", could the ini entry be placed in a ...
>
> [HOST=]
>
> section in the ini file? [2]
>
> If so, could this be the best solution?

The speed of hidef is accomplished by having only MINIT and MSHUTDOWN
handlers; it would be interesting to see how speed is affected by
moving some logic to the request init/shutdown; it should still have
the benefit of constants being defined at compile time.

>
> Regards,
>
> Richard.
>
> [1] http://shwup.blogspot.com/2010/04/about-constants.html
> [2] http://docs.php.net/manual/en/ini.sections.php
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling

---

## #48696 — Frederic Hardy — 2010-06-09T08:51:36+00:00

URL: https://externals.io/message/48339#48696 · In reply to: #48352

> Hi,
>
> 
>
> > […]
> Unfortunately this doesn't tell, what you actually measured. And note
> that as of 5.3 the "class constant" syntax can be used in the global
> scope, too.
>
>
> > […]
> No. We have a const keyword. Rather somebody interested in this topic
> might try to come up with a patch to at least allow "constant
> expressions" this should serve most needs.
>
> constant expressions might be stuff like __DIR__.'/foobar.php' or
> CONST_A | CONST_B.
>
> We already have two steps in the constant resolution for constructs like
> these:
>
> php>  class A { const C = FOOBAR; }
> php>  define('FOOBAR', 42);
> php>  echo A::C;
> 42
>
> See zval_update_constant_ex in zend_API.c as a starting point. While
> such an approach would also require some parser work and some clever
> idea where/how to store the expression to be evaluated etc.
>
> johannes
>
So, it's possible ?
Very good news.

Best regards,
Fred.

-- 
========================================================================
Frédéric Hardy : Architecte d'application/Admin. système/Ergonome
========================================================================

---

## #48697 — johannes@schlueters.de — 2010-06-09T09:36:17+00:00

URL: https://externals.io/message/48339#48697 · In reply to: #48696

> > […]

> So, it's possible ?
> Very good news.

Everything is "possible". The question is how maintainable the solution
would be, how much of an performance impact it has etc. That I don't
know. It might turn out not to be acceptable. I just gave a starting
point in case somebody has the time needed for some experiments.

As always: Ideas, there are many. They need developers who have time and
motivation to do it.

johannes
