Fork me on GitHub

Watch

GitHub Core

Show your support for Zikula! Sign up at Github account and watch the Core project!




GitHub Modules

Forum Activity

Forum feed

» Visit forum | » View latest posts

Module Dev Question about using includes  Bottom

  • I am developing a module and trying to make it portable by using classes. I basically want to include those classes in the pninit, pntables, and user/admin files. Is there a special way to do this? I am just using:

    Code

    $ModName = basename( dirname( __file__ ) );
    require_once("modules/$ModName/classes/classes.inc.php");


    and doesn't seem to be working. Any suggestions?
  • The include in your code is relative to the location of the current file so if the file that this call is from is in your module then the code should look like

    Code

    require_once("classes/classes.inc.php");


    You don't actually need to evaluate the module directory.

    -Mark
  • Not sure if that is true as modules are loaded through index.php right?
  • chrisabruce

    Not sure if that is true as modules are loaded through index.php right?


    As an entry point yes. But __FILE__ still evaulautes to the actual file your in.

    I know this to be the case as i've recently written a module utilising a class. See this file in CVS for an example.

    -Mark
  • markwest

    I know this to be the case as i've recently written a module utilising a class. See this file in CVS for an example.


    mark, I'm wondering if this is still the case as I think I've found the newest vrsion of the referenced file above here and the require_once statement seems to be different than you outlined.

    I bring this up because I am having similar troubles with a mod I am working on.
  • When I left out the 'modules/Recipe/...' in my Recipe module, the includes worked OK 95% of the time. Had to put it back in. This was for class include files also.

    You might display your include path to make sure it is what you expect it to be.
  • yeah - well, it appears to be going to the root of the harddrive!
    Something like

    Code

    require_once("classes/myclass.php");
    looked in the root of the drive - not even the root of the web server! eek!
  • To bring this up to date on this issue my current recomendations are to look from the perspective of the root file that will launch your module. This will always be either index.php or modules.php so reference all inlcudes from this point. The way to look at this is that all files including your module code are 'included' into index.php (or modules.php) at some point so reference all includes from the site root.

    There are some php.ini settings that mean you can do refer to it from the current file but this will not translate accurately across PHP installs. As Craig has pointed out i've changed this mailer code since this post. This is to reflect my further experience on this issue.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Thanks Mark! - quick followup question:

    in the pninit file for a mod, can I use something like:

    Code

    $path="/modules/$module/foo/bar";
    If so, how do I get $module? Since pninit runs from the "modules" module, I get "modules" from pnModGetName()...

    thanks!
  • As an API compliant module can't be renamed due to the function calls containing the module name then I would suggest simply putting the module name here. I can't think of any 100% automated way of doing this. There are ways that will work some of the time but not 100% hence the way the API compliant structure works.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • thanks, Mark, for the quick answer. You are an officer and a gentleman... :)
  • I've know several people that would dispute that comment...... ;)

    Seriously though i'm looking through the pnCopiliot module at the moment and will have some feedback for you by the weekend.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • thanks - that's what I'm working on and why this issue came up. icon_smile my co-developer/friend is insisting that

    Code

    $ModName = basename( dirname( __file__ ) );
    require_once("modules/$ModName/classes/classes.inc.php");
    is the better solution than hardcoding the mod name - your thoughts?
  • As Mark said, the Module name can;t change with out code changes, so it can be hard coded.

    --
    Home Page | Find on Facebook | Follow on Twitter
  • You can use that as it will work but given each function name in a API compliant module is <modname>_* (e.g. modname_userapi_get or modname_admin_modifyconfig) then you don't gain anything from doing so. To rename the module requires a search and replace on the module name anyway.

    -Mark

    --
    Visit My homepage and Zikula themes.</modname>

This list is based on users active over the last 60 minutes.