SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Non-Lexically Scoped Variables

    Hello fellow Sitepointers!

    I've had great responses through these forums so I thought I'd try my luck again.

    Basically, I'm a Perl programmer starting to pick up PHP, which is why I can code, but have all these silly little language specific questions that I have trouble finding answers for

    I am siting up a CMS for personal use and have a setup that involves having a main index file, having several modules (or classes as you all call them), and then having a specific file included and a function executed depending on the query string.

    This is all simple enough. However, I want to be able to use all of the classes in all the files including other classes and the file that gets included. Furthermore, I need it to be the same instance of the object so that it contains the same data. I tried doing so, but the it seems the object I created is lexically scoped (or only useable in the same file).

    I was just wondering how to expand the use of that object to all other files included and loaded through the main index. Any help would be greatly appeciated!

    Thanks for your time!

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I know that in Perl you can add your own module repositories to the @INC array by using use lib(). Is this where your problem is arising?

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really. I'll use an example.

    in the index.php file, I have something like the following:
    PHP Code:
    <?php
      
    include '_mod-bin/Module.php';
      
    $mod = new Module;
     
      include 
    '_inc-bin/a.php';
     
      
    b();
    ?>
    and in file a.php, I would have:
    PHP Code:
    <?php
      
    function b()
      {
        
    $mod->something();
      }
    ?>
    Module.php would just have some object stuff.

    Now, I can't use the object I created in index.php in the a.php file, but I woud like to. Thanks.

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    couldn't you just extend the parent class to include the new function, thus allowing objects to be ported across fine.

    class add_functs extends module
    {
    function b()
    {
    // all objects of class module are accessible here
    }
    }

    something like that anyway -
    teckis - that's news to me.

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand your right, some general tips;

    - For including class files best practice is to use the function require_once() - if a file is already included it will be ignored if any other script attempts to include it again. Note that require_once results in a fatal error if the file can't be found. The function include_once() will complain if it can't find the file but script execution will continue.

    - In general, when including files which in turn include other files, it's important to know that PHP will attempt to include files relative to the original script where execution began. This can cause confusion to start with but the best solution is to set your "include_path", which is a setting in php.ini, so that it also has a path to a directory where you keep all your files to be included. You can also set this with .htaccess files or perhaps even the ini_set() function (haven't tried the latter).

    An example with .htaccess;

    .htaccess file;
    Code:
    php_value include_path ".:/home/username/lib/"
    ( Note the : is the seperator above and first path "." is important to allow relative include paths from your scripts )

    Now in the directory /home/username/lib/ you might have;

    /home/username/lib/database/ << database classes
    /home/username/lib/application/ << application logic classes

    So, for example, you have index.php in /home/username/www;

    PHP Code:
    // index.php
    require_once('application/news.php'); // Includes the script /home/username/lib/application/news.php 
    PHP Code:
    // news.php
    require_once('database/dbi.php'); // Includes the script /home/username/lib/application/dbi.php for the database connection 
    As far as scope goes, PHP doesn't regard an included file as being a seperate scope to the one that included it. Any variables (globally) created in an included script are available to the script that included it after the inclusion occurs.

    In general there are just three scopes in PHP; global, functions and classes.

    Hope that helps.

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more point (just saw your example);

    PHP Code:

    <?php
      
    function b()
      {
        
    $mod->something();
      }
    ?>
    Inside function b() we're now in the function scope which does not have access to the global scope, by default. In general it's better practice to pass global variables to the function like;

    PHP Code:
    <?php
      
    function b(& $mod)
      {
        
    $mod->something();
      }
    ?>
    Note that the & operator is to pass by reference. I've got some notes on how references work in PHP here.

    But you do have the option of;


    PHP Code:
    <?php
      
    function b()
      {
        global 
    $mod;
        
    $mod->something();
      }
    ?>
    The latter tends to lead to headaches eventually though.

  7. #7
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That reminds me (sorry - overwhelming you with info) - in the Jan 2003 edition of the Perl review there's a good article (at the end) comparing Perl to PHP from a programmers perspective: http://www.theperlreview.com/Issues/v0i7.shtml - basically a "crossover" reference.

  8. #8
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why References are Important

    For most situations, using a copy of a variables contents is fine. But by copying a variables contents, what do we do if the origional variable changes after it has been passed?
    Origional is spelled original. ...

    (Good article though.)

    Just thought I'd point this article out, on copying variables in PHP3 vs PHP4+:

    http://zend.com/zend/art/ref-count.php.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  9. #9
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HarryF - Thanks a lot for your time to respond. You've pushed me in the right direction. I'm looking forward to that article. At any rate, I attempted doing things with the global variable (I'd like not to have to call in the objects into every single function) but it didn't work out when I tried. I could have been doing something wrong though.

    Is there any way to include the included files into the scope of the index file, or no? I know it's bad programming, but this is for a personal project that takes very little traffic

    Again, thanks for the help!

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any way to include the included files into the scope of the index file, or no? I know it's bad programming, but this is for a personal project that takes very little traffic
    Seems you can. I've never used this before but worked fine;

    PHP Code:
    <?php
    // index.php
    function test () {
        include(
    'helloworld.php');
        echo ( 
    'From helloworld.php from test() scope: '.$message.'<br />' );
    }

    test();
    echo (
    'From helloworld.php from global scope: '.$message.'<br />' );
    ?>
    PHP Code:
    <?php
    // helloworld.php
    $message='Hello World!';
    ?>
    This spits out;

    Code:
    From helloworld.php from test() scope: Hello World!
    
    Notice: Undefined variable: message
    From helloworld.php from global scope:
    So when I try to access $message from the global scope it can't find it. Obvious I guess - a hack I've never used


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •