0

I am trying to get figure this out and haven't been able to. I'm trying to create a function that will include different config files into the script when needed. I want to store all the variables in arrays and then use a function to include them.

Example

Config.php

$array = array(
'var1' => 'something',
'var2' => 'something else'
);

uses.php

class uses{

      public static function file($directory, $file){

        if(is_dir($directory))
        {   
            if(is_file($directory.$file))
            {        
           include $directory.$file;

            }
            else
            {
             echo "File Not Found $directory$file";
            }
        }
        else
        {
           echo 'Dir Not Found';
        }
    }
}

index.php after I've included uses the file

uses::file(Config.php);
print_r($array);

I know if you include a file inside a function they won't reach past the scope of the function. This would be loaded in by an auto loader so I would be able to use it anywhere inside my script.

Thanks in advance, if you need any more information please let me know.

  • What is your question exactly – crafter Oct 14 '13 at 05:19
  • Anthony, this is a very important concept in your understanding of PHP, so I suggest you go back to your PHP notes for this. http://php.net/manual/en/language.variables.php Having said that, variables don't have automatic file scope. They are normally local to the context level they are defined in (and first use sometimes implicitly defines them). include directives on the other hand just serve as code replacement tools, and allow you to break up your code. It has no influence on the scope of variables than if you were to include all resulting code in a single file. \ – crafter Oct 14 '13 at 05:20
  • How exactly is the function "file" related to the array? – Lloyd Banks Oct 14 '13 at 05:27

2 Answers2

0

Take a look at this: http://php.net/manual/en/function.parse-ini-file.php

But in simple terms you could so this:

config.inc.php

$config['var1'] = 'Hello';
$config['var2'] = 'World';

Then in your php:

include_once('config.inc.php');

or you could use this for an INI style include file: http://php.net/manual/en/function.parse-ini-file.php

config.inc.php

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

Then in your php:

$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);
Lionel Morrison
  • 566
  • 4
  • 15
0

It appears to me you may be going about this the wrong way. But first off the problems with the code you presented. Your code appears to be asking for two parameters but you are only passing in one. This will make your is_dir / is_file calls always fail because both of the conditions will never be true at the same time so your file isn't include.

Also you don't appear to be assigning the returned value to anything that has a lifetime greater then that of run time of your static function so even if the file was included your config variable would end up being thrown away.

To clean up your existing code you could do something along these lines.

<?php
class Config {
    static public $config;

    public static function load($path, $file){
         if (file_exists($path.$file) && is_file($path.$file)){
             self::$config = include ($path.file);
         }
    }
}

Then you would change your included file to return the data instead of assigning it to a variable. Like

<?php
return array(
    'var1' => 'something',
    'var2' => 'something else'
);

Then you would be able to use it like this.

Config::load("/path/to/config/", "Config.php");
echo Config::$config["var1"];

While this will make the code behave the way you are trying to do it this is probably a bad idea. You should be using Dependency injection within your other classes instead of invoking the static property with your other classes. This will afford you a seam when doing testing and also allow your methods and classes to document in the constructor and method signatures exactly what is needed to perform a given task. See this video for more information on what DI is, and this google clean code talk to help understand why using statics like this is a bad idea.

Orangepill
  • 24,500
  • 3
  • 42
  • 63