Location: PHPKode > scripts > Simple YAML Config Reader > simple-yaml-config-reader/settings.class.php
//this class uses SPYC class written by Chris Wanstrath <http://spyc.sourceforge.net/> to parse YAML files
//$l contains language-specific strings and is global for the application
global $l;

   * Settings - a simple class to load settings from a YAML file
   * @version 0.1.1 -- 2006-12-01
   * @author Chavdar Yordanov
   * This class uses the Singleton pattern; to initiate it, call its singleton() method,
   * for example $settings=Settings::singleton();
   * All configuration settings are accessible from the public array 'get' or through the getter method 'get'
   *   for example echo $settings->get["language"]; and echo $settings->get("language"); are equivalent,
   *   but the public variable can be called in order to change its values
   * IMPORTANT: config file must be located in the same folder as this script, the localisation files too
   * configuration files must be named after the server on which they reside, for example:
   * 'localhost.php' - at the developer's machine, and 'somehost.org.php' at the server 'www.somehost.org'
   * this allows to have several configuration files, which will be switched automaticaly by the class
   * Configuration files are in YAML format, but use .php extension - see configuration file format;
   * include("classes/_settings.php");
   * $settings = Settings::singleton();
   * echo $settings->get("database_name");
class Settings {
    //path to the settings file in YAML format
	public $config_file = ""; //stores the full absolute path to the configuration file; set in getConfigFile
    //path to the localization file
    public $localization_file=""; //stores the relative path to the localization file in a relevant language
    private $yaml_extension=".yml"; //configuration files file name extension

    //array to hold all the settings
	public $get=array();
	// Hold an instance of the class
    private static $instance;

	// The singleton method - call it to create an instance
    public static function singleton()  {
	   if (!isset(self::$instance)) {
		   $c = __CLASS__;
		   self::$instance = new $c;
	   return self::$instance;

	//class constructor; loads the settings only once per script and sets some common variables
	private function __construct() {
        if (!file_exists($this->config_file)) die ("Configuration file not found. Check if it is located in the same folder as the settings class file and is named properly.");
        //load the initialization file in YAML format and parse it using the SPYC class method
		$this->get = Spyc::YAMLLoad($this->config_file);

        //append some additional parameters to the settings list - you can add more here:
		$this->get["siteurl"] = $_SERVER['HTTP_HOST'];
		$this->get["scriptname"] =basename($_SERVER['SCRIPT_NAME']);

        //this is only really neccesary in multilanguage sites; but it is still a good idea to leave the last line there
        //check page language version, if any; set default language if language is not specified in the query string

    //returns the absolute path to the folder containing this script
    protected function getAbsolutePath() {
        //return dirname(__FILE__);   <-- can be used if the developer's machine and production server use the same OS
        //determine the path separator according to the OS
            $sep="/";  //the script is running on a xNIX machine
            $sep="\\"; //the script is running on a windows machine
        $path = explode($sep,__FILE__); //break into an array
        array_pop($path); //kill the last element, which contains the file name
        return implode($sep,$path).$sep; //put the path back together, including the trailing path separator

    //calculates the absolute path to the configuration file;  it should be located in the same folder as _settings.php
    protected function getConfigFile() {
        //get the config name from the server name
        $filename = str_replace("www.","",$_SERVER["SERVER_NAME"]).$this->yaml_extension;
        //echo self::getAbsolutePath();
        return self::getAbsolutePath().$filename;

    //check the language of the current page and load localisation strings from the YAML file
	protected function checkLanguage() {
		global $l;
        $query_lang=""; //will take the parameter from the query string or form
        if(isset($_REQUEST[$this->get["langparam"]])) {
        //if query string does not contain language parameter, get the default language from settings
		if($query_lang=="") {
		} else { //else use it as a language code for the current script
		$this->localization_file = self::getAbsolutePath().$this->get["lang"].$this->yaml_extension;
		if(file_exists($this->localization_file)) $l = SPYC::YAMLLoad($this->localization_file); //write the parsed output to the global 'l' variable for easier use

    public function get($setting) {
        return $this->get[$setting];

Return current item: Simple YAML Config Reader