user2663914
user2663914

Reputation: 65

Php Constant that depend from HTTP_HOST

I would like that my constant depend from the value of HTTP_HOST

 if ($_SERVER['HTTP_HOST'] == 'localhost') {
    const DB_HOST = 'localhost';
    const DB_USER = 'root';
    const DB_PASS = 'andrea00';
    const DB_NAME = 'ciaociao';
    } else {
    // db www.corsocomputer.com
        const DB_HOST = 'localhost';
        const DB_USER = 'root';
        const DB_PASS = 'andrea00';
        const DB_NAME = 'ciaociao';
    }

I have this, this code is ok.

But I would like write this with a class like

class Context {

    const NomeAttivita = 'test test test';

}

but I don't know how write with class

Upvotes: 1

Views: 861

Answers (3)

Yuri Blanc
Yuri Blanc

Reputation: 645

I would recommend to make a php file, like a configuration file. Then use define() to explicit define a constant. These will be available for all scripts including it.

Making a class like this:

   class Context {
    const DB_HOST = 'localhost';
    const DB_USER = 'root';
    const DB_PASS = 'andrea00';
    const DB_NAME = 'ciaociao';
}

Will make the variable accessible only to class instances and childs.

For you context class you may import your "db settings" file, and for you database connection class you may set the class variables from the config file in class constructor:

The config file:

define ("DB_HOST", "localhost");

the class:

class database{
private $dbHost;

function __construct() {
$this->dbHost= DB_HOST;

} 

In that way when you need your constants you simply include them, and you can add them to multiple classes. If you change the configuration, the change will reflect to all your application. Hardcoding the define() in the class is a bad practice.

  • defined constant (with define) will be accessible by their defined names CONSTANT_NAME statically.

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

You could define the constants globally like this perhaps in a config file and access them within the class as below. Usually the database details will be different on development and live servers, hence using this syntax.

define( 'DB_HOST', $_SERVER['HTTP_HOST']=='localhost' ? 'localhost' : 'server1.domain.co.uk' );
define( 'DB_USER', $_SERVER['HTTP_HOST']=='localhost' ? 'root' : 'dbo12345' );
define( 'DB_PASS', $_SERVER['HTTP_HOST']=='localhost' ? 'andrea00' : 'xyR40VklAp01M' );
define( 'DB_NAME', $_SERVER['HTTP_HOST']=='localhost' ? 'ciaociao' : 'db12345' );

class Context {
    const db_host=DB_HOST;
    const db_user=DB_USER;
    const db_pwd=DB_PASS;
    const db_name=DB_NAME;

    public function __construct(){

    }

    public function showconstants(){
        echo self::db_host, self::db_user, self::db_pwd, self::db_name;
    }
}

$ctx=new Context;
$ctx->showconstants();
$ctx=null;

Upvotes: 1

HenryTK
HenryTK

Reputation: 1287

You can use define() to define a constant which will have global scope, and therefore will be accessible in your class. Using global scope should be done carefully and not too often, but for something like the host name it can be appropriate:

define('HTTP_HOST', $_SERVER['HTTP_HOST'])
class Context {
    function doSomethingWithHost(){
        echo HTTP_HOST;
    }
}

Upvotes: 0

Related Questions