Require config file in class I want to use the variables that have been declared in a config.php file in a class for testing. I am not sure if this is the right way like I have done it.

I have a configuration file, config.php:

class Config {

    public $hosts = array(
        'a' =&gt; '192.168.1.10'
        'b' =&gt; '192.168.1.11',
    );

    public $ports = array(
        'a' =&gt; 22,
        'b' =&gt; 22,
    );
}


These are the variables I want to include in a file called Tests.php:

require_once 'config.php';

    class Test extends PHPUnit_Framework_TestCase
    {
        protected $hosts;
        protected $ports;
        ...

        public function __setUp()
        {
            $config = new Config();
            $this-&gt;hosts = $config-&gt;hosts;
            $this-&gt;portsSsh = $config-&gt;ports;
            ...
        }


        public function testConnect()
        {
            $session = $this-&gt;connect($this-&gt;hosts['a'], $this-&gt;ports['a']);
            $this-&gt;closeConnection($session);
        }
    ...
    }


Is it right doing it like this? I have the impression it is too complicated. I have to make a class for the config.php file and in the test class I have to do like a mapping $this-&gt;hosts = $config-&gt;hosts. 

That I don't like so much, because I have about 14 variables and first I have to declare them all and later I have to assign them all in the setUp() method. Isn't there a shorter or simpler way? I was thinking of using a loop and I don't want to use globals.

I wonder if I should do it like this:

require_once 'config.php';

        class Test extends PHPUnit_Framework_TestCase
        {
            protected $hosts;
            protected $ports;
            ...

            public function __setUp()
            {
                $this-&gt;config = new Config();
            }


            public function testConnect()
            {
                $session = $this-&gt;connect($this-&gt;config-&gt;hosts['a'], $this-&gt;config-&gt;ports['a']);
                $this-&gt;closeConnection($session);
            }
        ...
        }
This 'problem' of yours can be solved using Separation of concern. Your Database class should not know about config and how it is loaded. It should simply ask for the stuff it needs. Neither should your Test class know how to connect. Your test class should test the Database class. And your database class should handle the 'connection failed' error of whatever library you are using.

For instance:

class DatabaseConnection
{
    public function __construct($host, $username, $password, $port='22'){
        if connection failed throw new ConnetionFailedException
    }
}


Your would then also have a config class that loads the correct config file. e.g.:

class Config
{
    public function __construct($enviroment)
    {
        $this-&gt;data = include __path_to_config_files_.'/'.$enviroment.'.inc';
    }
}


Your could for instance use a __get method to retrieve the data instead of having a getParameter.

your config file would look something like this:

__path_to_config_files_/development.inc

&lt;?php

return array(
    'key' =&gt; 'value'
);


And your test class would then:

$config = new Config('development'); //or whatever enviroment you are in
try {
    $connection = new DatabaseConnection($config-&gt;db['host'], $config-&gt;db['username'], $config-&gt;db['password']);
} catch(ConnetionFailedException) {
    //connection failed
}
//connection success