views:

25

answers:

4

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
                )
        )

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

A: 

Look up "topological sort".

Ignacio Vazquez-Abrams
A: 

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.

Techpriester
All modules are classes :) and I use autoload but Don't understant how you mean?
sandelius
If you already use autoloading, I don't see your problem. Autoloading should should resolve all your dependencies on demand automatically and you won't need any includes at all.
Techpriester
A: 

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.

Gordon
Including all dependencies is a waste of performance in most cases because you quickly build up long include lists of which many php-files are nether really used.Autoloading resolves this.
Techpriester
There is nothing in the example you could tell from whether it is wasting resources or not. Plus, I also said use autloading.
Gordon
A: 

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.

sandelius
So, you are basically looking for Dependency Injection Service Containers? In this case, see http://components.symfony-project.org/dependency-injection/
Gordon