views:

499

answers:

2

Hello,

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?

+11  A: 

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
vartec
This is the way I would suggest doing it.
Mark Davidson
This is the solution i was looking for.Without the namespaces of course!Tnx
sanders
Wouldn't that be \constants\EH_MAILER, or at the very least constants\EH_MAILER?
Adam Backstrom
@Adam: You're right.There's been a lot of discussion about which will be namespace separator, in early versions it was :: (like in C++)
vartec
@Adam: http://wiki.php.net/rfc/backslashnamespaces
vartec
A: 

What php version are you using?

See php's page for class constants

jcinacio