1

What is the best way to make a variable accessable to all classes.

For example, I want to have a configuration file (Call it config.php) that is going to have a variable like so:

$server_url = "www.myaddress.com";

And I have a main library type file that contains a bunch of classes that need to access the $server_url. So here begins that main library file:

require 'config.php';

class one {
  function a() {
          $html = "<a href='$server_url/main.php'>LINK</a>"
         return $html;
         } 

  function b() {
        $html = "<a href='$server_url/about.php'>LINK</a>"
        return $html;

         }
}


class two {

    function new() {
          $html = "<a href='$server_url/blah.php'>LINK</a>

     } 
}

What would be the best way to make $server_url from the config.php available to every function? Or at least available to all the functions in a class?

1
  • Start learning about Dependency Injection Commented Jan 18, 2012 at 19:41

5 Answers 5

3

Personally I would use a static entity to hold all configuration values.

Usually, most php applications have a single entry point (index.php) that can load up the config.php file and make the static entity available from that point.

If your application has multiple entry points, then you will need to include config.php in each of these points.

Something like this:

<?php
    class Configurator{
        private static $_configuration = array();
        public static function write($key, $value) {
            self::$_configuration[$key] = $value;
        }
        public static function read($key) {
            return self::$_configuration[$key];
        }
    }
    Configurator::write('server', 'http://localhost');
    Configurator::read('server');
 ?>

CakePHP has a similar class: http://api.cakephp.org/view_source/configure/

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, a static entity. Forgive my ignorance, but how do you make a static entity?
1

Make the config into a class in itself and use a static methods either along the line of serverUrl() or get('server_url'). Then call them like any other static methods to classes (I'll choose the latter in this example):

$html = "<a href='" . Config::get ('server_url') . "/main.php'>LINK</a>";

The config class could be pretty slim, use a constructor like:

public function __construct (array $config)
{
  foreach ($config as $key => $value)
  {
    $this->$key = $value;
  }
}

And add the get() method along these lines:

public function get ($key)
{
  return $this->$key;
}

This way you can read the config from an array that you can have as a separate, actual config file, and reuse the same code for multiple projects.

You'll also be able to access the variables from anywhere in the project and you'll get a sort of pseudo-namespacing (in case the project needs to run on an older version of PHP).

Please, don't copy the code verbatim, it's written as an example.

Comments

1

think of globals are evil. Try to use design patterns to get access to some configs globally.

I'm a big fan of singletons to get global access to objects, arrays or other data-types.

<?php

class st {
    static $_this;


    function __construct(){
        self::$_this = $this;
    }

    static function &getInstance(){
        return self::$_this
    }

    static function set($key, $value){
        self::$_this[$key] = $value;
    }

    static function &get($key){
        return self::$_this[$key];
    }
}

// Usage

new st();

st::set('foo', 'bar');

// In some class
st::get('foo'); //return 'bar'

// Or when there are some classes/objects
st::getInstance()->foo->bar();
$st =& st::getInstance();
$st->foo->bar();
?>

Roughly wrote down a small singleton, but don't know whether there is a syntax error.

While handling with getInstance it's certain that you define the variable by reference =&

Comments

0

Define a constant in config.php like:

define('SERVER_URL', '...');

In your class:

echo SERVER_URL;

Comments

0

What works for me the best is to use a config file like config.ini And then to use $my_config = parse_ini_file(file path/config.ini');

Now everywhere in my code including inside functions and classes, I will use The PHP superglobal like this: $GLOBALS["my_config"]['my_global_var']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.