0

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
  • 1
    Are you trying to load your class, so that all classes can use it? Commented Jul 26, 2009 at 10:40
  • How are you trying to include it Commented Jul 26, 2009 at 10:47

3 Answers 3

2

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.

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

Comments

1

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

Comments

0

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).

Comments

Your Answer

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