Stop using `global` in PHP

后端 未结 6 1937
醉梦人生
醉梦人生 2020-11-22 03:45

I have a config.php that is included to each page. In config I create an array that looks something like:

$config = array();
$config[\'site_name\         


        
6条回答
  •  长情又很酷
    2020-11-22 04:29

    I've solved this with a class:

    class Config
    {
        public static $SiteName = 'My Cool Site';
    }
    
    function SomeFunction
    {
        echo 'Welcome to ' , Config::$SiteName;
    }
    

    fcortes' suggestion to use constants is also a good one. I'd only like to suggest to give all constants a prefix, like CFG_SITE_NAME, so to avoid accidental name clashes with other constants.

提交回复
热议问题