views:

21

answers:

4

I'm having problems with this class definition because of the definition of $directories. Please help:

<?php
 .....
 class Config {

   public static $directories = array(
      "resources" => realpath(__DIR__),
       "root" => $_SERVER['DOCUMENT_ROOT'],
       "branch" => $_SERVER['DOCUMENT_ROOT'] . "/branch",
       "templates" => realpath(__DIR__ . '/templates'),
       "library" => realpath(__DIR__ . '/library'),
       "views" => realpath(__DIR__ . '/views'),
       "controllers" => realpath(__DIR__ . '/controllers'),
       "backups" => realpath(__DIR__ . '/backups')
    );

  }
?>
A: 

You can't call functions while declaring class/object variables/constants. You also can't use string concatenation. Its pretty limited.

UPDATE: You can use the following workaround (it took a little time to write), its a little ugly because it requires defining constants twice:

<?php

define('CONFIG_DIRECTORY_RESOURCES', realpath(__DIR__));
define('CONFIG_DIRECTORY_ROOT', $_SERVER['DOCUMENT_ROOT']);
define('CONFIG_DIRECTORY_BRANCH', $_SERVER['DOCUMENT_ROOT'] . '/branch');
define('CONFIG_DIRECTORY_TEMPLATES', CONFIG_DIRECTORY_RESOURCES . '/templates');
define('CONFIG_DIRECTORY_LIBRARY', CONFIG_DIRECTORY_RESOURCES . '/library');
define('CONFIG_DIRECTORY_VIEWS', CONFIG_DIRECTORY_RESOURCES . '/views');
define('CONFIG_DIRECTORY_CONTROLLERS', CONFIG_DIRECTORY_RESOURCES . '/controllers');
define('CONFIG_DIRECTORY_BACKUPS', CONFIG_DIRECTORY_RESOURCES . '/backups');

 class Config
 {
   public static $directories = array(
      "resources" => CONFIG_DIRECTORY_RESOURCES,
       "root" => CONFIG_DIRECTORY_ROOT,
       "branch" => CONFIG_DIRECTORY_BRANCH,
       "templates" => CONFIG_DIRECTORY_TEMPLATES,
       "library" => CONFIG_DIRECTORY_LIBRARY,
       "views" => CONFIG_DIRECTORY_VIEWS,
       "controllers" => CONFIG_DIRECTORY_CONTROLLERS,
       "backups" => CONFIG_DIRECTORY_BACKUPS,
    );
  }
AlReece45
A: 

Quoting from the PHP manual:

This [property] declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

You could define an empty array, and then call a method to populate it at runtime, or a singleton class which initialises the array values in the constructor

Mark Baker
+3  A: 

You can't initialize static members to anything other than direct literals and already defined constants. You could use something like this though:

<?php
class Config 
{
    static protected $directories = null;

    static public function getDirectory($dirName)
    {
        if (self::$directories == null)
        {
            self::$directories = array(
                "resources" => realpath(__DIR__),
                "root" => $_SERVER['DOCUMENT_ROOT'],
                "branch" => $_SERVER['DOCUMENT_ROOT'] . "/branch",
                "templates" => realpath(__DIR__ . '/templates'),
                "library" => realpath(__DIR__ . '/library'),
                "views" => realpath(__DIR__ . '/views'),
                "controllers" => realpath(__DIR__ . '/controllers'),
                "backups" => realpath(__DIR__ . '/backups')
            );
        }
        return self::$directories[$dirName];
    }
}
#EOF
Kris
This looks like a clean solution. Are there any negatives to it?
Tunji Gbadamosi
there might be a slight overhead in calling a method (and doing a check) instead of accessing a static property, but I can basically guarantee you'll never be able to notice it. Not unless you start doing nothing but accessing Config in a loop for millions of times anyway.
Kris
A: 

why introduce the overhead of an object? registries have their place but these are prime candidates for constants so why not just define them?

ToonMariner