Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php define() Constants Inside Namespace Clarification

People also ask

How can you define PHP constants?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

Can you redefine constants in PHP?

No, you cannot redefine a constant (except with runkit_constant_redefine), that's why is called CONSTANT.

Which keyword define a constants variable in PHP?

Constants can be defined using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.

Are PHP constants case sensitive?

Constants are case-sensitive. By convention, constant identifiers are always uppercase. Note: Prior to PHP 8.0.


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


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