Best way to allow plugins for a PHP application

前端 未结 8 726
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 09:12

I am starting a new web application in PHP and this time around I want to create something that people can extend by using a plugin interface.

How does one go about

8条回答
  •  执念已碎
    2020-11-27 09:33

    You could use an Observer pattern. A simple functional way to accomplish this:

    
    

    Output:

    This is my CRAZY application
    4 + 5 = 9
    4 * 5 = 20
    

    Notes:

    For this example source code, you must declare all your plugins before the actual source code that you want to be extendable. I've included an example of how to handle single or multiple values being passed to the plugin. The hardest part of this is writing the actual documentation which lists what arguments get passed to each hook.

    This is just one method of accomplishing a plugin system in PHP. There are better alternatives, I suggest you check out the WordPress Documentation for more information.

提交回复
热议问题