I've coded a small PHP engine which includes some abstract classes. The user of the engine must implement subclasses of these abstract classes, and supply their subclasses to the engine. The engine has to be able to create and manipulate instances of these subclasses, so the user can't just create an instance and pass it to the engine.
So I need a way for the user to pass their subclasses to the engine. One obvious way would be to force the user to give these subclasses a specific name and put them in a specific directory, but that just seems inelegant somehow. Knowing that PHP could store classes in variables, what I tried first was to store the classes in constants, so I had a file with a few define
s that user would fill in with the names and paths of their subclasses. That didn't work - it would seem PHP cannot store classes in constants, only variables.
So now I'm trying the same approach, but with variables instead of constants. There's a file, UserIncludes.lib.php
, in which the user is required to set two variables to the names of their subclasses. Then those variables are used to call those subclasses throughout the engine's code. I'm not sure this is a good system, particularly because of the need to import the variables into every function that needs them using the global
keyword, increasing the clerical and mental load on the engine.
So what are best practices for doing this sort of thing in PHP? It seems like it must be a fairly common task.