How does codeigniter implement the hook mechanism? , friends in need can refer to it.
I remember the last time I went to Xila for an interview, the interviewer asked me a question: How does codeigniter implement the hook mechanism?
I couldn’t answer it at the time, but after I came back and checked some information, I figured it out, so I’ll record it here:
The codeigniter hook is implemented as follows: first, load the Hooks class in line 122 of the framework's core file system/core/CodeIniter.php, and then define several mount points in the file, such as pre_system (line 129) , post_controller_constructor (line 295), etc., and execute the _call_hook() method of the hooks class on these mount points.
Attached is the source code of codeigniter’s hooks class:
可以看出codeigniter实现钩子机制的方式不够优雅,其实完全可以使用观察者模式来实现钩子机制,将挂载点当做监听的事件。 |