7

I came across this Wikiepedia article which says that using interfaces for defining constants is not a good thing.

Solution suggested there is to define constants in a final class and use static import. That works for Java. I come from the PHP world which does not have the "import" thing. So, what is the acceptable solution for PHP?

2
  • 2
    sounds like a techical question, maybe it should be asked on stackoverflow.com?
    – user281377
    Feb 25, 2011 at 14:43
  • 1
    @user281377 sounds more like hair splitting, this might be the right place.
    – ZJR
    Dec 17, 2012 at 6:03

3 Answers 3

3

Just use class constants.

class ConstClass
{
    const SOME_CONSTANT = 10;
}

echo ConstClass::SOME_CONSTANT;

As of PHP5, the final keyword is also supported.

3
  • If i want to use this constant in another class, should i extend the ConstClass or should i just 'include' and access it within the class that i need it. If i take the 'include' route is it right approach with regard to unit testing? I remember reading that we should not use variables/constants from global scope inside a class for proper unit testing.
    – Srisa
    Feb 26, 2011 at 8:46
  • @Srisa: You can simply use require_once to load your classes. Since it's a const, I don't see how it would affect unit testing. Also, take a look at auto loading classes. Feb 26, 2011 at 14:01
  • Thank you. Looks like i misunderstood some things about unit testing.
    – Srisa
    Feb 28, 2011 at 9:48
2

You are referring to an article that say's

In the Java programming language, the constant interface pattern describes the use of an interface solely to define constants, and having classes implement that interface in order to achieve convenient syntactic access to those constants. However, since constants are very often merely an implementation detail, and the interfaces implemented by a class are part of its exported API, this practice amounts to putting implementations details into the API, which is considered inappropriate. In general, collecting system constants into classes independent of behaviour, might create a poor object-oriented design, because it is often a sign of low cohesion. It is for these reasons that implementing constants interfaces is considered to be an anti-pattern.

I come from Java, and I am doing PHP right now ... From my point of view, as long as you use interface constants which are of internal system nature, you are fine... once you start exposing your interface constants as part of your external API, you shoud do that using classes not interfaces.

Another thing to consider is serialization, and deployment packages. If you need constants as part of the deployment package to enable an error free de-serialization remember: You can add implementation details into classes, but not into interfaces.

Sadly PHP only knows public,protected and private to control the namespace, and to control the scope of constants. In Java you have package-private on top, which basically says "in the same directory"/"java package de.*".

Remember this when comparing Java to other languages and make conclusions regarding ant-patterns which refer to OODesign in general and across languages. It may lead to wrong conclusions ;-) ...

I don't say the wikipedia article is wrong... but it is not 100% correct either.

Just my 2 €cent

1

You import the class =)
No, there is no "import" in PHP, but you must be including the source files for your classes somehow. Just include the constant-defining class.

I suggest to make the class abstract and use class constants.

2
  • What if i want to use these constants in other classes? Should i just include the class and use those constants in other classes? Is that okay from unit testing perspective? We'll be using something inside the class that is from the global scope.
    – Srisa
    Feb 26, 2011 at 5:07
  • Using class constants will probably be "cleaner" than using "define" global constants, since they are encapsulated in the class.
    – Gipsy King
    Feb 28, 2011 at 12:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.