Php define() Constants Inside Namespace Clarification

Using namespaced constants is fairly easy but you must use const keyword.

Then you can directly call the constant using backslash \:

namespace Dummy\MyTime;
const MONTHS = 12;
const WEEKS = 52;
const DAYS = 365;


namespace Test;
use Dummy\MyTime;

$daysPerWeek = MyTime\DAYS / MyTime\WEEKS;
$daysPerMonth = MyTime\DAYS / MyTime\MONTHS;

echo "Days per week: $daysPerWeek\n"; // 7.0192307692308
echo "Days per month: $daysPerMonth\n"; // 30.416666666667

I think this is cleaner than using define.

Having said that, what you want (assign a scalar expression to a constant) will work only if you are using PHP >= 5.6:

namespace Information;
const ROOT_URL = 'information/';
const OFFERS_URL = ROOT_URL . 'offers/';

namespace Products;
const ROOT_URL = 'products/';
const OFFERS_URL = ROOT_URL . 'offers/';

namespace Test;
$info_offers_url = \Information\OFFERS_URL; // information/offers/
$prod_offers_url = \Products\OFFERS_URL;    // products/offers/

I hope this will help you.

Source: http://php.net/manual/en/migration56.new-features.php


If you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace. The following examples which I tried will make it clear.

The following code will define the constant "CONSTANTA" in the global namespace (i.e. "\CONSTANTA").

<?php
namespace mynamespace;
define('CONSTANTA', 'Hello A!');
?>

if you want to define constant for a namespace you can define like

<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>

Otherwise, you can use const to define a constant in the current namespace:

<?php

namespace NS;

define('C', "I am a constant");
const A = "I am a letter";

echo __NAMESPACE__, , PHP_EOL; // NS
echo namespace\A, PHP_EOL; // I am a letter
echo namespace\C, PHP_EOL; // PHP Fatal error:  Uncaught Error: Undefined constant 'NS\C'

Taken from the Manual