Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts

    Accessing Variables From A Class Function Include

    Hi,

    I was curious if there is a way to access variables that are in an include inside of a class function from within a script that was included inside another class function... Kind of hard to explain but hopefully the example helps. The following example produces an undefined variable: var error in the programming file, which it should as it doesn't have access to the variable. Is there a way to pass that to the next script without making it a global variable or using a definition?

    Example:

    Data file to include
    PHP Code:

    $var 
    'test'
    Programming file to include
    PHP Code:

    echo $var
    PHP Code:

     
    class Test {

      public function 
    __construct() {
        
    self::load_data();
      }

      public function 
    load_data() {
        include 
    'data-file.php';
      }

      public function 
    process_data() {
        include 
    'programming-file.php';
      }

    }

    $test = new Test;

    $test->process_data(); 

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Well unfortunately you, as far as I know, need to either declare it in the global scope or use a definition. That doesn't mean you can't use some tricks to make it easier, though. Let's take a look at the not example:

    Say your file programming-file.php has three variables:

    PHP Code:
    <?php

    $apples 
    "fruit";
    $oranges "delicious";
    $pasta "noodle";

    ?>
    In your process_data method you'll need to do:

    PHP Code:
    <?php

    public function process_data() {
         global 
    $apples$oranges$pasta;
         include 
    'programming-file.php';
    }

    ?>
    However that doesn't mean you need to spend a bunch of time declaring variables in the global scope. Since I use a custom template system I often need to send output to a file. So what I'll do is store everything in an array. Example:

    Instead of the way we saw above, we do this:

    PHP Code:
    <?php

    $globalVars
    ['apples'] = "fruit";
    $globalVars['oranges'] = "delicious";
    $globalVars['pasta'] = "noodle";

    ?>
    Then all we need to do in process_data is:

    PHP Code:
    <?php

    public function process_data() {
         global 
    $globalVars;
         include 
    'programming-file.php';
    }

    ?>
    Other than that the only thing that I think will accomplish it is something like a register_globals() which is a very insecure method to use.

    Hope this helps!
    Last edited by ShaneC; 01-16-2011 at 09:16 PM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Thanks for the reply... That's what I kind of figured. I was trying to add custom modules into my CMS I made. I will probably do something like that. Possible add this to the Test class just was hoping for another way.

    PHP Code:

    public function module_vars ($mod_name$array) {
        
    $this->mod_vars[$mod_name] = $array;



  •  

    Tags for this Thread

    Posting Permissions

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