tags:

views:

488

answers:

3

Hi,

I've the following class that I wish to include in all my other classes, but when I use the include or include_once commands my PHP page fail.

<?php
/**
 * Configuration class
 *
 * @author LennieDG
 * 25 July 2009
 */
class CConfig
{
    public static final $DB_HOST = "...";
    public static final $DB_NAME = "...";
    public static final $DB_USERNAME = "...";
    public static final $DB_PASSWORD = "...";
}
?>
+2  A: 

You cannot use final for properties. It's only allowed for classes and methods.

Make sure you've set error_reporting and display_errors properly while in development.

Philippe Gerber
+1  A: 

Just a sidenote:

Properties cannot be declared final in PHP. Only classes and methods can be declared final. To achieve this using PHP, you should use class constants.

PHP5 - the final keyword: http://us.php.net/manual/en/language.oop5.final.php

PHP5 - class constants: http://us.php.net/manual/en/language.oop5.constants.php

Lior Cohen
A: 

Since all of your properties are static, they can be accessed anywhere by doing

CConfig::$DB_HOST

But that isn't recommended, as it has many of the same problems are using global variables. Another option is to not make the values static and to pass an instance to any class that needs it (this is known as dependency injection).

$config = new CConfig();

// Either via the constructor
$obj = new OtherObject($config);

// or a setter
$obj->setConfig($config);

This makes testing much easier (as you can mock the CConfig object) and makes the other objects more reusable. It does mean you need to have access to the CConfig instance wherever you intend to use it though. This can be handles by using something like the registry pattern, or a dependency injection container (but that's maybe getting a little advanced).

Brenton Alker