Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
Including a module & variable scope ?
  • Posted: 28.11.2005, 10:57
     
    littlefoot
    rank:
    Softmore Softmore
    registered:
     November 2003
    Status:
    offline
    last visit:
    20.03.06
    Posts:
    54
    If I include a module in a block, why is the variable scope not accessible as it should be?
  • Posted: 28.11.2005, 13:55
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    Can you be more specific as the problem as it's difficult to advise otherwise.

    -Mark
  • Posted: 28.11.2005, 21:31
     
    littlefoot
    rank:
    Softmore Softmore
    registered:
     November 2003
    Status:
    offline
    last visit:
    20.03.06
    Posts:
    54
    Sure thing...

    For example, I have a core/php block with the following:

    Code

    $testname = pnUserGetVar('name');
    include("http://www.mysite.net/pn/index.php?module=Test");



    Lets say modules/Test/index.php has the following:

    Code

    <?php
    echo $testname;
    ?>


    This should work right?
  • Posted: 28.11.2005, 21:51
     
    littlefoot
    rank:
    Softmore Softmore
    registered:
     November 2003
    Status:
    offline
    last visit:
    20.03.06
    Posts:
    54
    Originally I had modules/Test/index.php with the following:

    Code

    <?php

    $testname = pnUserGetVar('name');

    echo $testname;

    ?>


    and I had a core/php block with just this:

    Code

    include("http://www.mysite.net/pn/index.php?module=Test");


    But, the block just ends up empty. If I go to the modules/Test/index.php
    directly, the ouput is there. Any idea why it won't show in the block?
  • Posted: 28.11.2005, 22:11
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    The problem is using an include via a URL. The way a URL based include works is different to the way a local include works and thus the scopes will be different. A local include will read the file and parse the source using the local PHP environment and will retain the variable scope. A remote include will only include the pre-parsed output since there is no way to remote get at the source of the include (nor should there be).

    This is covered in the PHP include docs.

    Thread moved to the development forums.

    -Mark
  • Posted: 29.11.2005, 00:32
     
    littlefoot
    rank:
    Softmore Softmore
    registered:
     November 2003
    Status:
    offline
    last visit:
    20.03.06
    Posts:
    54
    Thanks for the info. I didn't realize there was a difference between a URL include and a local include.

    I've changed the include to this:

    Code

    include("modules/Test/index.php");


    and all works great now.
  • Posted: 29.11.2005, 02:33
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    In addition to variable scoping an include via http will be a lot slower since you've got to go all the way through the network layer (httpd, ip stack, NIC etc.) where as a local include is a simple file system access.

    -Mark

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula