This question is about PSR-0 autoloaded libraries and the way main configuration constants should be declared and used.
From your point of view (lib/framework developer), where/how should be declared your library configuration PHP constants (such as ESCAPE_SOMETHING
, USE_DOM
, API_URL
, ...) in such a way that
- your code can easily address them
- client code can easily modify them depending on needs
Is there any convention, recommendation, or even something I missed in PSR-0 I should be aware of?
-
I am wondering if they could go directly into the file which contains the autoloader logic itself. However, I do not like the fact that people will have to edit this file to modify configuration.Jérôme– Jérôme2014年01月09日 14:35:48 +00:00Commented Jan 9, 2014 at 14:35
1 Answer 1
This question is about PSR-0 autoloaded libraries
I think you're conflating two different issues. Finding and loading the PHP code for FooClass
is (or ought to be) a different problem from how new FooClass()
or FooClass::staticThing()
should behave.
Ideally, FooClass
shouldn't care whether I (the guy using the library) relied on an autoloader or whether I hard-coded my own require_once()
statement.
library configuration PHP constants
One ground-rule first... never with constants from define()
. Polluting the global namespace is a bad idea, and IIRC even the new namespace features are a little weird in that regard. Class-constants on the library classes are acceptable, but as the library-consumer you probably aren't hand-editing those.
From your point of view (lib/framework developer), where/how should be declared your library configuration PHP constants
You mean, how would I prefer for consumer code to set options that affect my code? I'd prefer something like:
FooLibraryConfigurator::set("a","b");
FooClass::doStuffThatNeededConfData();
In other words, configure my library based on a mechanism I created for you to use. :p