1
\$\begingroup\$

I am building a PHP framework and would like to get some feedback on a few different sections of the project so far. I consider myself still a neophyte in PHP so I would like to ask if I'm going about completing these different tasks in an efficient and or correct way.

This section is of the system configuration file (.ini). Similar PHP core configuration file it is meant to hold values used through out the framework; for instance default MySQL database connection values. The class also can define values of and array as constants. The reason for creating the feature was to combine values used through out the framework in one place in a familiar format.

I have posted the System class, ini file and script to show how it is used.

System Class

class System{
    //Path to core.ini file
    private $inipath = 'core.ini';
    private $inifile;
    protected $parseini;
    private $arrvalue;

    //Instantiation 
    public function __construct(){
        //Path to core.ini file
        $this->inifile = $this->inipath;
        //Parse the ini file
        $this->parseini = $this->ParseINI();
    }
    /**
     * Parse system configuration file
     * @param void
     * @return Array - multidimensional array
     */
    public function ParseINI(){
        //Path to core.ini file
        //Moved to the method because construct
        //is not used
        if(!file_exists($this->inifile)){
            trigger_error("System file not found",E_USER_ERROR);
        }
        return parse_ini_file($this->inifile,true);
    }

    public function GetValue($value = null){
      if(!empty($this->parseini) && isset($value)){
        return $this->parseini[$this->arrvalue][$value];      
      }
      return $this->parseini[$this->arrvalue];  
    }

    public function SetValue($value){
        $this->arrvalue = $value;
    }

    public function DefineINI(){
        if(isset($this->arrvalue)){
            foreach ($this->GetValue() as $key => $value) {
                define(strtoupper($key),$value);
            }
        }
    }

    //End Scope
    public function __destruct(){
        //Empty
    }
}

core.ini file

[system_paths]
DIRECT_URL = "http://localhost/fw/";
ROOTDIR = "root/";
FILESDIR = "root/files/";

;System database connection parameters
;Used for interaction with the systems database
[sys_db]
host = "localhost";
user = "root";
password = "password";
database = "fwsystemdb";

;Default connection parameters
[database_connection]
db_host = "localhost";
db_user = "root";
db_password = "passowrd";
db_database = "dummy";

;Session settings
[session_settings]
session_localsave = false;
session_dir = "http://localhost/fw/root/sessions";

;Error Loggin
[error_log]
log_activity = false; 
log_file = "http://localhost/fw/root/systemlog";

Script using System Class

$system = new System();
//Set array [system_paths] to search for value
$system->SetValue('system_paths');
//Get valuse DIRECT_PATH from file
echo $system->GetValue('DIRECT_URL ');
//Define array values as constant
$system->DefineINI('system_paths');

Any feedback would be appreciated.

\$\endgroup\$
3
\$\begingroup\$

Some improvements(they would save your[and framework users] a lot of time on debug):

  • All system wide components should implement pattern Singleton, this would guarantee that you have only one System at the same period.
  • Try to create interface SystemInterface, this would allow you to create ServiceStub pattern(It's very useful on testing)
  • Split your class and move parser to another class(use Strategy pattern). Your system should depend on configuration, not file! Also this would allow you to create another config-files like .json or even .php
  • Configuration shouldn't be stored in applications main class, use ConfigurationRegistry for this(pattern Registry)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.