I was thinking about writing a modular script (For fun/practise) and was thinking about how it is best to allow modules to affect the core-code. I have some ideas on how to do it but I'm unsure how secure it is / whether it's the best/most efficient way to do it.
Hooks:
If I have a variable (or property) with an array where the key is a describing name and is set to a function name:
$hook_<hookname>['<function description>'] = '<function name>';
And then whenever is appropriate for the hook to be called I foreach through the array and execute each function in turn, passing through appropriate variables for the hook.
Class Overriding:
I figure that at some point or another I'll need to override a class from a module, naturally I can extend the class, however that doesn't change the class objects are created from. So I could try two strategies..
Use a hook right after an object has been created, rename the object and create a new object from the new class with the same properties or whatever. I'm not sure if there's an easy way of moving across the properties to an object with a different class. The problem with this method is that if two modules use the same hook to override the class then only one module will be working.
The other method involves having classes register themselves on a global array on initialisation (likely via a function), then modules can override classes using a function which will handle errors and replace the value in the array for the class it's overriding if there isn't a problem, this has the benefit of allowing modules to not just fail and allows for error handling to make them "shutdown" so to speak. The array would probably then be stored as a protected property so that it's difficult for malicious code (Either user-injected or otherwise) to override it after initialisation then whenever the main class is needed you can use something like:
$object = new $classregister->getClass('superclass')(); //Perhaps a second variable for the function could always return the original function?
This still has the problem of two modules wanting to override the same class but at least this way there won't just be errors because only one of the modules classes didn't run, there's also the potential that the overriding class can have built in error-handling, recognise that the class that previously overrode the main class does not conflict and then extend and override that instead.
For the class thing I'm fairly certain there's a better way of doing it but I can't think of anything.