sanders
sanders

Reputation: 10898

constants class in php

I once heard it's good to have one class with all your application constants so that you have only one location with all your constants.

I Tried to do it this way:

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

and

 class constants{
         const EH_MAILER =1;
 }

But both ways it doesn't work. Any suggestions?

Upvotes: 3

Views: 3083

Answers (2)

vartec
vartec

Reputation: 134631

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

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 there's better way to do it. Namespaces.

consts.php

<?php
namespace constants
const EH_MAILER = 1

...

other.php

<?php
include_once(consts.php)

$mailer = \constants\EH_MAILER

Upvotes: 18

J.C. Inacio
J.C. Inacio

Reputation: 4472

What php version are you using?

See php's page for class constants

Upvotes: 0

Related Questions