views:

85

answers:

3

I've come across a Registry Class and I'm wondering whether to bother with this or just go constants, or are there separate uses for site-wide global variables such as database connection information, website URI, etc?

Here's the class I came across:

<?php

    Class Registry {


     private $vars = array();

     public function __set($index, $value)
     {
            $this->vars[$index] = $value;
     }


     public function __get($index)
     {
            return $this->vars[$index];
     }

    }

    ?>

Basically just a class that has an array with magical getters/setters. Are there any disadvantages with this code as opposed to using constants?

A: 

By constants I assume you mean named variables.

Anyway the advantage of the class in your example is that you now have an array of all the variables in your Object which you can loop through and you can store a variable amount of data.

The disadvantage is that you now have an Object when looking at it tells you nothing about what is stored and what it does. In case of a class named "Registry" this might be logical, but for something called a "Car" it isn't.

Memory wise the most efficient method probably depends on how you use the object.

Les
+1  A: 

The biggest advantage of config classes compared to a bunch of constants if that you can have multiple configs (for different environments) which can extend each other, e.g.

 // define default Config and custom ones for specific environments

 class BasicConfig {
     function __construct() {
          $this->db_driver = 'mysql';
          // other params that are common to all configs
 }

 class TestConfig extends BasicConfig {
     function __construct() {
          parent::__construct();
          $this->db_user = 'root';
          $this->db_password = '';
          // other params for test config
 }

 class ProductionConfig extends BasicConfig {
     function __construct() {
          parent::__construct();
          $this->db_user = 'liveuser';
          $this->db_password = 'secret';
          // other params for production config
 }

 // change configuration depending on where we're running
 if($_SERVER['SERVER_NAME'] == 'local')
     $config = new TestConfig;
 else
     $config = new ProductionConfig;

 // the main application doesn't care which config we're using
 $application->init($config);
stereofrog
Oh, that's pretty nice. So just to clarify would you still use any constants, or completely store everything in that class?
meder
depends on the project type, for example, should the configuration be editable by non-programmers.
stereofrog
+1  A: 

Although you may have a small overhead when using a registry, I would advice one to use it.

  1. It will help you debugging as you have one common place to get all your information.
  2. It's more secure, because users cannot add their own values by manipulating URLs. register_globals will be turned off most times, but you aren't in control, if a webspace provider stores you website.

As an alternative you could create your own registry, so that you can make it's content more explicit by defining your own getters and setters.

Christian Strempfer