开发者

Order include file depending on inherit

Hi there people and greetings from Sweden!

I have a really tricky problem here. I'll try to show exactly what I mean.

I'm building a modulebased CMS, and some modules inherit from a parent module. My problem is that the parent modules need to be included before the "children".

I fetch moduleinfo from a XML file and store it in an array like this:

Array
(
    [bloggy] => Array
        (
            [module_id] => blog
            [module_name] => Blog
            [module_desc] => Description
            [module_url] => http://www.url.se
            [author] => Dev Name
            [author_url] => http://url.se
            [version] => 1.0
            [inherit] => core|twitter
            [path] => /path/to/file
            [dependon] => Array
                (
                    [0] => core
                    [1] => twitter
          开发者_如何学Python      )
        )

I've done a explode on inherit and saved it into "dependon" as you see above. The problem now is, how can I sort which order to include the files. Every module inherits from core but if there is another module in the depenon array then the "child" module must be included after.

I hope you understand what I mean?

// Tobias


Look up "topological sort".


You could build you modules as classes and then use the __autoload magic function to automatically include/require all needed php files.

That way it's much less of a headache when you have complex dependencies.

Refer to the PHP manual for details on autoloading.


Just include all dependencies in your files. Try

// module1.php
require_once 'core.php'

// module2.php
require_once 'core.php'
require_once 'module1.php'

// module3.php
require_once 'core.php'
require_once 'module1.php'
require_once 'module2.php'

Including module3 will also include module2, module1 and core. You could leave out the core and module1 in module 3 and it would still load them all, but then you have to know what includes which.

Or use autoloading and don't bother about it.


Hmm I'll try to explain a little better.

I search the module folder for the xml info file and save the data into an array, I also save the path to the file in the array.

Then I use a foreach loop to include and instantiate the modules. My problem is that the parent modules must be instantiated before the "children".

I do not want to touch the core files when adding a new module, I need to use the hooks on a parent module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜