17
Quote:
I did not realize that classes and functions defined inside a module would be global. Is that a good idea, in general?
This is something that PHP does... we really have no choice in the matter. That is why we have all the naming rules for functions, class, language constants etc... it is so that when we process blocks etc from several modules, that we don't get collisions when we include their classes and functions.
Quote:
Regardless, absolutely you'd have to be careful since they are defined globally. Could you maybe provide examples of how to use the 'if function_defined', 'if class_exists' to conditionally load files? I would think that 'if function_NOT_defined' would be the way to go, but I don't know how to do that, either.
Also, could you use "include_once" or something like that instead?
include_once might work, depending on how the module is implemented. If your cloned module shares the *same* files then this would be good enough (i.e. 'news2' actually includes files from the 'news1' directory). If they have the same files, but each has their OWN copy of the same files, then this won't help because PHP considers them as distinct files.
However, most of this is irrelevant because most people write their functions with hard-coded dirname-dependent things. For example a function 'news_get_all_articles' might contain something like: $sql = "SELECT * from xoops_news_articles", etc... In the cloned module we need xoops_news2_articles, so basically we need a *copy* of the function but we'd have to name it 'news2_get_all_articles' so both functions can be used together. Basically this requires everything to be duplicated, but with change of 'news' to 'news2' everywhere.
The alternative is to write modules such that the database table 'xoops_news_articles' is not hard-coded, but somehow looked up every time it is needed. Then two modules could share one copy of the code and still function independently. This would be very tricky, and XOOPS may not support enough core functionality to do this.
I'm not sure my suggestions of 'function_defined' are all that useful, but here's a little info. This is a built-in PHP feature:
if (!function_defined('news_get_all_articles')) {
// load some file containing this function
}
Sorry for the confusing post... hopefully it helps a bit
. If you have any questions about it, let me know...