Jump to content


Photo

Class function require() problem


  • Please log in to reply
6 replies to this topic

#1 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 03 April 2006 - 09:57 PM

PHP Platform: 4
I currently have a class with a function LoadResource($res)

That does
ob_start();
require_once($res);
ob_end_clean();
To be sure no headers are sent (This function is to be called on function includes and etc.

accessed ie $class->LoadResource('file.php');

Now heres the problem. My structure is in this format:

<?
require('configfile'); // this has info needed for constructing the class
//Say one variable is $theme that holds the theme the user wants to use
$class = new Class;
$class->LoadResource('test.php');
test.php:
<?
if($theme) die('Theme is set'); } else { die('theme is not set!!!!'); }

Problem: file is loaded just fine, except the functions file does not have access to the variables initiated.
The example above prints theme is not set.


I was hoping using require inside of a class would act like a normal require, but its not :(

I know i COULD use global $theme in the LoadResource, but this code is to be used in many things, theres many variables, and the variable is to change per application.

So is there anyway I can make LoadResource() have access to read/set global variables?
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#2 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 03 April 2006 - 10:20 PM

Woot found a use for Extract() :)

Got it working.
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#3 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 03 April 2006 - 10:48 PM

Grr... still not completely working.

the included file has access to the variables, but my index/other functions does not.

class.php:
class classx {
 function LoadResource($res) {
   extract($GLOBALS);
   require_once($res);
 }
}
index.php:
$b = new classx;
$b->LoadResource('functions.php');
echo 'Variable:' . $variable; // prints nothing for $variable
test();  // prints nothing after variable:
functions.php:
$variable=1;
echo 'File Variable: ' . $variable; // prints 1
function test() {
 global $variable;
 echo 'Function Variable:' . $variable; // prints nothing for var
}

[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#4 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 11:08 AM

anyone :(?
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 04 April 2006 - 02:47 PM

You need to read up on [a href=\"http://au3.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]scope[/a]. Basicaly, objects have there own namespace, any variables or any files included within them are only available within each object.

#6 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 07:35 PM

[!--quoteo(post=361577:date=Apr 4 2006, 10:47 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Apr 4 2006, 10:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You need to read up on [a href=\"http://au3.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]scope[/a]. Basicaly, objects have there own namespace, any variables or any files included within them are only available within each object.
[/quote]
I understood that, Im asking how can I get past it :(
extract globals gives my included file access to variables
but any variables in the included file are not global, How can i make them global?
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#7 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 09:59 PM

[innocent bump]
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users