Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8252635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:36:42+00:00 2026-06-08T00:36:42+00:00

<?php define(‘ABSPATH’, dirname(__FILE__)); //Absolute path to index /* * Method 1 * Dependency Injection

  • 0
<?php
define('ABSPATH', dirname(__FILE__)); //Absolute path to index

/*
* Method 1
* Dependency Injection
*/
class Config{

    private $_config = NULL;
    private $_filepath = NULL;

    public function __construct($filepath){
        $this->_filepath = $filepath;
        $this->load();
    }

    private function load(){
        if ($this->_config === NULL){
            if (!file_exists($this->_filepath)){
                throw new Exception('Configuration file not found');
            }else{
                $this->_config = parse_ini_file($this->_filepath);
            }
        }
    }

    public function get($key){
        if ($this->_config === NULL){
            throw new Exception('Configuration file is not loaded');
        }
        if (isset($this->_config[$key])){
            return $this->_config[$key];
        }else{
            throw new Exception('Variable ' . $key . ' does not exist in configuration file');
        }
    }
}

function getLost($where, $why, $who){
    //do smth
}

try{
    $config = new Config(ABSPATH . '/app/config.ini');
    getLost('here', 'because', $config->get('who'));    
}catch(Exception $e){
    echo $e->getMessage();
}
?>

<?php
/*
* Method 2
* Config is accessed via static class
*/

class Config{

    private static $_config = NULL;
    private static $_filepath = NULL;

    public static function load($filepath){
        if (self::$_config === NULL){
            self::$_filepath = $filepath;
            if (!file_exists(self::$_filepath)){
                throw new Exception('Configuration file not found');
            }else{
                self::$_config = parse_ini_file(self::$_filepath);
            }
        }
    }

    public static function get($key){
        if (self::$_config !== NULL){
            throw new Exception('Configuration file is not loaded');
        }
        if (isset(self::$_config[$key])){
            return self::$_config[$key];
        }else{
            throw new Exception('Variable ' . $key . ' does not exist in configuration file');
        }
    }
}

function getLost($where, $why){
    $who = Config::get('who');
}

try{
    Config::load(ABSPATH . '/app/config.ini');
    getLost('here', 'because');    
}catch(Exception $e){
    echo $e->getMessage();
}
?>

<?php
/**
* Method 3
* Config variable needed is passed as function parameter
*/
$config = parse_ini_file(ABSPATH . '/app/config.ini');

function getLost($where, $why, $who){
    //do smth
}

getLost('here', 'because', $config['who']);
?>

<?php
/*
* Mathod 4
* Config is accessed inside a function via global
*/
$config = parse_ini_file(ABSPATH . '/app/config.ini');

function getLost($where, $why){
    global $config;
    $who = $config['who'];
}

getLost('here', 'because');
?>

Which of these variants is the best practice solution? If none, please provide your variant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:36:43+00:00Added an answer on June 8, 2026 at 12:36 am

    I would go for variant 1 (dependency injection).

    Variant 2 uses static methods which are as already stated just enother way of global methods. And we all know that is bad right? right?

    Variant 3 isn’t me favorite because it’s not OOP enough ;-), but serious: what if you (or the person using your code) wants to change the format of the config file?

    Variant 4: global…

    So basically my issues with other options are: testability, tight coupling, global.

    So variant 1 it is. I would also create an interface for the config class so you may add a different class for your config stuff at a later point. E.g. you (or someone else for that matter) wants to use XML files for config.

    Another thing I would change is private stuff. If someone wants to extend the class he/she wouldn’t have access to the variables this way. My rule of thumb (not sure if everybody agrees with this though) is only make stuff private if you want people to have access to it (e.g. it will change at some point). The danger of using private is that people will get around it by hacks to do what they want.

    Read more about SOLID and see the Google clean code talks for more info.

    Just my 2 cents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it's all in index.php /* Define site root */ defined('DOCUMENT_ROOT') ? null : define('DOCUMENT_ROOT',realpath(dirname(__FILE__)));
Imagine I have this constant in PHP: define('APP_PATH', str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) .
I have a problem with the below:- //index.php <?php define('PATH','Ajfit/'); /* Get all the
bootstrap.php define('ROOT_DIR', dirname(__FILE__)); require_once('Zend/Controller/Front.php'); $front = Zend_Controller_Front::getInstance(); $front->addModuleDirectory(ROOT_DIR . '/application/modules'); $front->dispatch(); application/modules/controllers/IndexController.php class IndexController
consider the following code scenario: <?php //widgetfactory.class.php // define a class class WidgetFactory {
In core.php I can define Configure::write('Routing.admin', 'admin'); and /admin/controller/index will work. but if I
i am using this code to define the directory paths of my project: config.php
I like to refer images, css and php files using their absolute path, that
config.php define('DB_TYPE', 'MYSQL'); dbManager.php incude_once('config.php'); switch ( DB_TYPE ) { case 'MYSQL': $this->_dataObject =
lets say we have a directory. project/index.php in index.php <?php require 'config/config.php'; echo ROOTPATH;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.