Tutorial :constants class in php



Question:

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?


Solution:1

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  


Solution:2

What php version are you using?

See php's page for class constants


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »