Answer the question
In order to leave comments, you need to log in
What are the ways to implement templates and hooks in PHP?
I know that there is Google, there are books and there is code. Without delving into the question, I caught a glimpse of a couple of articles that are more confusing, or seem not quite right.
Therefore, I would like to ask for explanations on the fingers and in simple words.
Examples are welcome.
1. How to implement the application of templates?
2. How to implement hooks?
I know for example that it is possible to do templates through the buffer.
Those. form a page, place it in the buffer, change it there and then output it. ob_start, ob_get_contents().
But with hooks it's more difficult. How can you write a function in the code and allow the user to add the code that will be executed before it, or after it. The only way I can imagine is to parse the files and compile the result into a separate php file, which will then be executed.
Answer the question
In order to leave comments, you need to log in
Use event dispatching.
For example - https://github.com/symfony/event-dispatcher
Make yourself 2 events - beforeCall and afterCall. That's all :)
Hooks can be done like this: get a list of all registered functions, filter them by name, those that fit - execute.
But it's a road to nowhere and it's not worth it.
As for the code before and after - a very strange case and also sooooo bad, you can't do that.
You get a list of tokens through token_get_all, then find a call specifically to your function that you concatenate before and after. Next, do what you need in eval.
But once again, this is an example of a terrible approach and you can’t write like that.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question