Sandokan
Sandokan

Reputation: 1105

How to create a global configuration file?

I'm trying to create a global configuration file in my php project, essentially, I have this content:

class SystemConfiguration 
{
   // Settings

   public static $base_url    = 'http://localhost/Application/';
   public static $db_host     = 'localhost';
   public static $db_name     = 'app';
   public static $db_username = 'root';
   public static $db_password = '123456';
}

now the content above is placed into config.php file, this file is in available in the root/settings/ folder. Now what I'm trying to achieve is get the variable included in the SystemConfiguration file from other file, what I tried, for example in the index.php:

 <?php
      require_once("system/config.php");

      $this->$base_url;

but when I load index.php I get fail to load response data in the network console tab, what I did wrong?

Upvotes: 0

Views: 85

Answers (1)

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

after including the folder has your class you can call its vars by doing an object from your class, in case static variables you can call directly like below:

SystemConfiguration::base_url;

where you can call static variables by :: and in case non static you call them by initialize object from your class:

$object_system_configuration = new SystemConfiguration();
$object_system_configuration->base_url;

Upvotes: 1

Related Questions