Constant class in php

I somehow heard well that I have one class with all your application constants, so you only have one location with all your constants.

I tried to do it as follows:

class constants{
    define("EH_MAILER",1);
 }

and

 class constants{
         const EH_MAILER =1;
  }

But in both cases this does not work. Any suggestions?

+3
source share
2 answers

In the current version of PHP, this is the way to do this:

class constants
{
   const EH_MAILER = 1;
}

$mailer = constants::EH_MAILER

http://www.php.net/manual/en/language.oop5.constants.php


Starting with PHP 5.3, the best way to do this. Namespaces.

consts.php

<?php
namespace constants
const EH_MAILER = 1

...

other.php

<?php
include_once(consts.php)

$mailer = \constants\EH_MAILER
+18
source

What version of php are you using?

See php page for class constants

0
source

Source: https://habr.com/ru/post/1705365/


All Articles