# Proposal for 2 new magic constants

- Thread: https://externals.io/message/130990
- Original archive: https://news-web.php.net/php.internals/130990
- Started by php@s22.us on 2026-05-23T16:38:48+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #130990 — php@s22.us — 2026-05-23T16:38:48+00:00

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

Hi all,

I would like to propose two new magic constants to PHP: __HOME__ and 
__USER__

These would allow us easy ways to get the following information in our 
scripts:

Constant			Results
__USER__			username
__HOME__			/home/username

To get that info now we have to resort to things like:

define('HOME_PATH', implode('/', array_slice(explode('/', __DIR__), 0, 
3)));
define('USER_NAME', explode('/', HOME_PATH)[2]);

Personally, I use these a lot and it would be nice to call a simple 
constant instead of a line of code.  I think others could benefit from 
them, as well, hence my proposal.

So, I'd like to hear what others think before presenting an RFC.  Do 
they sound beneficial to anyone else?

Thanks,
Marc

P.S.  Hopefully I'm posting this to the correct list.  If not, please 
point me to where I need to be.

---

## #130991 — Jordi Kroon — 2026-05-23T17:21:22+00:00

URL: https://externals.io/message/130990#130991 · In reply to: #130990

> I would like to propose two new magic constants to PHP: __HOME__ and 
> __USER__

I strongly oppose this idea.

This kind of information is environment-specific and does not really 
belong as a language-level magic constant. Existing magic constants 
describe source context (`__FILE__`, `__DIR__`, etc.), not host or user 
environment details.

For CLI usage, this information is usually already available through 
environment variables such as `HOME` and `USER`, which is the more 
appropriate place for this kind of information.

In isolated environments such as containers or chroots, exposing user or 
home-directory information is also something we generally should avoid.

More importantly, I think we should discourage PHP web applications from 
reaching into other parts of the system and instead encourage them to 
stay within their application/web directories.

> P.S.  Hopefully I'm posting this to the correct list.  If not, please 
> point me to where I need to be.

This is the correct list. Often before starting an official RFC an 
e-mail is sent to this mailing list to see if a certain topic/idea has 
interest.

-- 
Regards,

Jordi Kroon

---

## #130994 — Andrey Andreev — 2026-05-23T18:45:20+00:00

URL: https://externals.io/message/130990#130994 · In reply to: #130991

Hi,



>
> This kind of information is environment-specific and does not really
> belong as a language-level magic constant. Existing magic constants
> describe source context (`__FILE__`, `__DIR__`, etc.), not host or user
> environment details.
>
> For CLI usage, this information is usually already available through
> environment variables such as `HOME` and `USER`, which is the more
> appropriate place for this kind of information.
>

I concur, and that settles the argument for me. However ...


> In isolated environments such as containers or chroots, exposing user or
> home-directory information is also something we generally should avoid.
>
> More importantly, I think we should discourage PHP web applications from
> reaching into other parts of the system and instead encourage them to
> stay within their application/web directories.


This default mindset of "PHP is for web and everything must conform to
that" is my biggest pet peeve on this list. It would serve PHP better to
not impede more general purpose use cases.

Cheers,
Andrey.

---

## #130998 — Larry Garfield — 2026-05-23T19:00:02+00:00

URL: https://externals.io/message/130990#130998 · In reply to: #130991

> 
>
> > […]
>
> I strongly oppose this idea.
>
> This kind of information is environment-specific and does not really 
> belong as a language-level magic constant. Existing magic constants 
> describe source context (`__FILE__`, `__DIR__`, etc.), not host or user 
> environment details.
>
> For CLI usage, this information is usually already available through 
> environment variables such as `HOME` and `USER`, which is the more 
> appropriate place for this kind of information.
>
> In isolated environments such as containers or chroots, exposing user or 
> home-directory information is also something we generally should avoid.
>
> More importantly, I think we should discourage PHP web applications from 
> reaching into other parts of the system and instead encourage them to 
> stay within their application/web directories.
>
> > […]
>
> This is the correct list. Often before starting an official RFC an 
> e-mail is sent to this mailing list to see if a certain topic/idea has 
> interest.
>
> -- 
> Regards,
>
> Jordi Kroon

I am also -1 here, for the reasons Jordi lists.  In cases where quick access to that information is relevant or useful, there are better alternatives to magic constants.

--Larry Garfield

---

## #130999 — php@s22.us — 2026-05-23T20:57:52+00:00

URL: https://externals.io/message/130990#130999 · In reply to: #130998

> In cases where quick access to that information is relevant or useful, 
> there are better alternatives to magic constants.

Larry,

What did you have in mind?  If it's better than the workaround that I 
posted in my original email, I'd love to hear it.  I'm just looking for 
a simple way to grab that information directly from PHP.

Thanks,
Marc

---

## #131000 — Andrey Andreev — 2026-05-23T21:25:08+00:00

URL: https://externals.io/message/130990#131000 · In reply to: #130999

Hi Marc,

getenv('HOME') and getenv('USER') are probably what you're looking for,
although HOME might be a non-Windows thing.
Both variables should also be available in $_SERVER.

Cheers,
Andrey.
