Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] PHP Error, should be working

Started by sathenzar 6 replies 800+ views
Original Post
sathenzar
sathenzar
Hey sorry for all the code that's going to be below but I just am frusterated beyond belief. This should be working. I have a file called template.php

<?php
 require_once( "./includes.php" );
 require_once( "./extras.php" );
 class gl_template
 {
 	 var $temp_dir;
 	 function gl_template($temp_file)
	 {
	 	 echo "ROOT: $fsRoot";
	 	 if($temp_file == "" || $temp_file == NULL)
		 {
		 	$temp_file = "$fsRoot/templates/default/";
	 	 }
		 /*$handle = fopen( "$temp_file/temp.cfg", "r" );
		 if(!$handle) { return(TEMPERROR_NOEXIST); }
		 if(!$contents = fread( $handle, filesize("$temp_file/temp.cfg")  ))
		 { return(TEMPERROR_READ); }*/
		 require_once( "$temp_file/temp.cfg" );
		 $dbc = mysql_connect( "$gl_host", "$gl_user", "$gl_pass" );
		 $p = mysql_query( "SELECT temp_dir FROM configt" );
		 $result = mysql_fetch_assoc($p);
		 $this->temp_dir = $result["temp_dir"];
		 echo $result["temp_dir"];
		 mysql_close($dbc);
	 }
	 
	 function parse_file($temp_file)
	 {
	 	 if(!$handle = fopen( $this->temp_dir . "/$temp_file", "r" ))
		 { return(TEMPERROR_NOEXIST); }
		 $contents = fread( $handle, filesize( $this->temp_dir . "/$temp_file" ));
	 }
	 
	 function testF()
	 {
	 	echo "TEST";
	 }
 }
?>
and then the magical file called extras.php

<?php
 $fsRoot = "http://localhost/drsuite/fsv2.0";
?>
I just want template.php to see $fsRoot in the extras.php which I have never had a problem doing before. So when I have test.php and create the gl_template class ($gl_tmp = new gl_template("");) it'll know what the current directory is. For some reason it's not working.
konForce
konForce
Without trying to figure out what you are attempting to do, it looks like you are missing calls to "global."
global $fsRoot;echo "ROOT: $fsRoot";
kanzler
kanzler
konForce is right about the need to use 'global' in your functions. Might I suggest using a 'define()' statement like this:

define('fsRoot', "http://localhost/drsuite/fsv2.0");

That way you can use the following in your functions without the need for 'global':

echo "ROOT: ".fsRoot;
sathenzar
sathenzar
Now I tried a different method and it's still not working. Why can't the classes see variables out side the class anymore? They've always been able to before. I always liked having my database variables on a different file so I could just change the settings in tehre and it would work. Now it seems that's been disabled or something.
<?php $fsRoot = "http://fsfsd";  class gl_template { 	 var $temp_dir; 	 function gl_template($temp_file)	 {	 	 echo "ROOT: $fsRoot";	 	 if($temp_file == "" || $temp_file == NULL)		 {		 	$temp_file = "$fsRoot/templates/default/";	 	 }		 /*$handle = fopen( "$temp_file/temp.cfg", "r" );		 if(!$handle) { return(TEMPERROR_NOEXIST); }		 if(!$contents = fread( $handle, filesize("$temp_file/temp.cfg")  ))		 { return(TEMPERROR_READ); }*/		 require_once( "$temp_file/temp.cfg" );		 $dbc = dbengine();		 $p = mysql_query( "SELECT temp_dir FROM configt" );		 $result = mysql_fetch_assoc($p);		 $this->temp_dir = $result["temp_dir"];		 echo $result["temp_dir"];		 mysql_close($dbc);	 }	 	 function parse_file($temp_file)	 {	 	 if(!$handle = fopen( $this->temp_dir . "/$temp_file", "r" ))		 { return(TEMPERROR_NOEXIST); }		 $contents = fread( $handle, filesize( $this->temp_dir . "/$temp_file" ));	 }	 	 function testF()	 {	 	echo "TEST";	 } }?>
tstrimp
tstrimp
Quote:
Original post by sathenzar
Why can't the classes see variables out side the class anymore?


Because it is very bad design?

<?php class gl_template { 	 var $temp_dir;         var $root;         __construct($root)  // PHP5         {           $this->root = $root;         }         gl_template($root)  // < PHP5         {           $this->root = $root;         } 	 function gl_template($temp_file)	 {	 	 echo "ROOT: $this->root";	 	 ~snip stuff~	 }	 	 function testF()	 {	 	echo "TEST";	 } }?>$fsRoot = "http://fsfsd"; $template = new gl_template($fsRoot);
ApochPiQ
ApochPiQ
The answer has been provided: you need to use global to import variables from the global namespace into each class function's namespace. That's standard PHP behavior. [edit](Even better, just use tstrimp's method. It's a far superior design.)[/edit]

It sounds like your PHP install might have previously had register_globals turned on. That is extremely bad. register_globals is the work of Satan and you should not write code that relies on that behavior. See here for more details.
sathenzar
sathenzar
Sorry, for some reason it didn't show the replies. That's really weird. I'll try what's been given. I tried globals before though and it said unexpected T_GLOBAL
Sander
Sander
You need to put the global in the functions in the class, not in the class itself.

/* wrong */class foo {  global bar;  function baz {  }}/* right */class foo {  function baz {    global bar;  }}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.