php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60355 namespace wide and specific constant
Submitted: 2011-11-22 07:54 UTC Modified: 2012-08-10 18:53 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: matty023 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-11-22 07:54 UTC] matty023 at gmail dot com
Description:
------------
It would be great if define() wasn't global throughout the whole app and contained inside the namespace to prevent collisions.

It could also be feasible to create a new type of constant that is available throughout an entire namespace;

define : Defines a named constant
= Global constant throughout execution

const  : Class Constants
= It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them. 


??     : namespace wide global
nsdefine
nsconst
also accessible via \my\namespace\MYCONSTANT
= It is possible to define constant values on a per-namespace basis remaining the same and unchangeable. Namespace Constants differ from normal variables in that you don't use the $ symbol to declare or use them. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-10 18:53 UTC] nikic@php.net
There is already a language construct to define constants in a namespace:

    namespace Bar;
    const FOO = 'bar';

Yes, `const` also works outside a class ;)

define() is a function taking a constant name, so it (just like all other functions accepting constant/function/class names) needs a fully-qualified name. So if you want to define a namespace constant with define() (instead of `const`) you have to provide the full name:

    define('Bar\FOO', 'bar');
 [2012-08-10 18:53 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 23:01:28 2024 UTC