Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define a php config file that define constant and use it any where in project

how to define a config file that i can use it any where whole project without include every where?

like image 732
hosein Avatar asked Jul 11 '12 07:07

hosein


People also ask

How do you define a constant in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

How do you define a constant in PHP What is the purpose of constant function?

If you have defined a constant, it can never be changed or undefined. To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name.

How can we declare variable and constant in PHP?

Constant vs VariablesA constant can only be defined using define() function. It cannot be defined by any simple assignment. A variable can be defined by simple assignment (=) operator. There is no need to use the dollar ($) sign before constant during the assignment.

Where do I put config PHP?

The config. php file, located in your /global folder contains the unique settings for your Form Tools installation: your database connection settings, root folder and URLs and other information. This file is the only file in the script that should be customized.


1 Answers

There are two ways,

  1. define() : The constants you define using define method is available to the whole project.

    define('SITE_URL' , '/path/to/docroot/');
    echo SITE_URL;
    
  2. Class constant : A class constant is available to the whole project.

    class MyConfig{
        const SITE_URL = '/path/to/docroot/';
    }
    echo MyConfig::SITE_URL;
    
like image 123
Shiplu Mokaddim Avatar answered Nov 14 '22 23:11

Shiplu Mokaddim