Jump to content

Plugin structure in php


root@localdomain
 Share

Recommended Posts

Hi all,

 

I am working on php project and willing to use the "everything as a plugin" idea so alot of people can easily contribute.

 

I am wondering how I can make a php page that includes my plugins, without having to hardcode them(as in require_once, ...).

And afterwards it should be possible to execute a function on each of the enabled plugins.

 

Especially the "hardcode" part I am not sure on how to fix it.

As for executing a function for each plugin (so that will be a Class object), I wonder how I can make an object of each class from an interface and then execute a function without hardcoding the creation of all objects.

 

Any one ideas or experience with something like this?

 

Thanks in advance.

Link to comment
Share on other sites

If you put all the plugins in one directory, you can

// includes a file without giving it direct access to any variables
// note that it could still use stuff like global $var;
function include_once_clean() {
    include_once func_get_arg(0);
}

foreach (glob("/path/to/plugins/*.php") as $file) include_once_clean($file);

From there it's up to your imagination.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for your suggestion.

 

In the end I implemented the includes using the database of the application.

 

So if you have a new plugin installed you need to run a scan script. It reads each subdirectory of plugins/ and gets a plugin info file.

The include statement, the class name and the plugin name are read and put in the database.

 

The admin gets a list of installed plugin and can enable them (uses a database column).

When the user calls a page that uses plugins, the installed and enabled plugins are used/shown. So the plugin php page gets included (as is in the plugin info file) and an object is created using the name that is also retrieved from the plugin info file (and put in the database when scanning of course).

 

Hope this helps anyone else that is trying to do the same.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.