Question

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')
    );

  }
?>
Was it helpful?

Solution

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

OTHER TIPS

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

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,
    );
  }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top