Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2011, 07:30 PM   PM User | #1
Coyote6
Regular Coder

 
Join Date: May 2009
Location: Moore, OK
Posts: 277
Thanks: 10
Thanked 41 Times in 41 Posts
Coyote6 is an unknown quantity at this point
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(); 
Coyote6 is offline   Reply With Quote
Old 01-16-2011, 08:13 PM   PM User | #2
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
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!
__________________
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

Last edited by ShaneC; 01-16-2011 at 08:16 PM..
ShaneC is offline   Reply With Quote
Old 01-16-2011, 08:49 PM   PM User | #3
Coyote6
Regular Coder

 
Join Date: May 2009
Location: Moore, OK
Posts: 277
Thanks: 10
Thanked 41 Times in 41 Posts
Coyote6 is an unknown quantity at this point
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;

Coyote6 is offline   Reply With Quote
Reply

Bookmarks

Tags
access, class, includes, undefined, variables

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:48 AM.


Advertisement
Log in to turn off these ads.