Page 1 of 1

Class variables not being used

Posted: Mon Jul 10, 2006 3:27 am
by mattcooper
Hi guys n gals...

Just starting to get to grips with using classes and have found a way of loading methodss into a main class so thar you can reuse it with different core functions. Problem is, the code (to me!) is a bit unweildy, and I'm having trouble working out why some of the variables aren't echoing out when a new object is created.

Class code:

Code: Select all

class mainClass {
		
		var $module; // FOR SUBCLASS FUNCTION LOADING
		
		/////////////// VARIABLES TO BUILD THE DISPLAY TABLE /////////////////////////////////////////////
		var $width;   ////////////////////////////////////////////////////////////////////////////////////
		var $height;  ////////////////////////////////////////////////////////////////////////////////////
		var $border;  ////////////////////////////////////////////////////////////////////////////////////
		var $rows;    ////////////////////////////////////////////////////////////////////////////////////
		var $content; ////////////////////////////////////////////////////////////////////////////////////
		var $data;    ////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////////
		
		function LoadClass($loadModule) { // AND THIS FUNCTION
     
			$this->module = new $loadModule; // Loads a new instance of the requested class (e.g "subclass" below)
		
		}
		
		function RunFunction($FunctionName) { // AND THIS FUNCTION TOO!!
       
           $this->module->$FunctionName();        
       
	   }
	   
	} // End of class

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	class subclass {
	
		function LoadModule($loadModule) { // AND THIS FUNCTION
     
			$this->module = new $loadModule; // Loads a new instance of the requested class (e.g "subclass" below)
		
		}
	
		function create_Frame ( ) { // create the framework for the table
		
				echo "<table border = ". $this->border ." width = ". $this->width ." height = ". $this->height .">\n";
			
				$this->LoadModule( "subclass" );
			
		} // end of function create_Frame ( )
		
		function add_Data( ) { // Add some data to the cells calling this function
			
			return $this->data;
			
		}
		
		function one() {
		
			echo "<b>Function 1 has been called</b><br />";
   		}

   		function two() {
       	
			echo "<b>Function 2 has been called</b><br />";
   		}

   		function three(){
       		
			echo "<b>Function 3 has been called</b><br />";
		}
		
		function create_Rows ( ) {
		
			$host = localhost;
			$db   = ]]]]]]]]]]]]]];
			$un   = ]]]]]]]]]]]]]];
			$pw   = ]]]]]]]]]]]]]];
			
			// mysql_connect ( $host,$un,$pw );
			mysql_connect ( $host,$un,$pw );
			mysql_select_db ( $db );
			
			$sql = "SELECT * FROM users";
			$result = mysql_query ( $sql ) or die ( "Could not perform query - ". mysql_error () );
			$rows = mysql_num_rows ( $result );
			
			$num = 1;
				
				while ( $num <= $rows ) {
				
					$data = mysql_fetch_assoc ( $result );
			
					echo "<tr><td>". $data[username] ."</td><td>". $data[notes] ."</td></tr>\n";
				
					$num++;
				
				}
		} // end of function create_Rows ( )
		
		function close_Table ( ) { // Close off the table create by create_Frame ( )
			
			echo "</table><br />\n";
			
		} // end of function
		
	} // End of subclass
Object code:

Code: Select all

$mainclass=new mainClass; // Instantiate the main class
	$mainclass->LoadClass("subclass"); // Load the subclass
	
	$mainclass->RunFunction('create_Frame');
	
	$mainclass->border = "1";	
	$mainclass->width = "80%";	
	$mainclass->height = "200";	
	
	$mainclass->RunFunction('create_Rows'); // Call a function defined in the subclass
	$mainclass->RunFunction('close_Table');
	
	$mainclass->RunFunction('two'); // this could create a button, insert an image, anything really!
Sorry there's so much of it... can anyone see if there's a glaringly obvious problem that would stop the table attributes being echoed?

Thanks in advance!

Posted: Mon Jul 10, 2006 3:37 am
by Benjamin
Maybe it's because your setting them after you call the create_Frame function :wink:

Posted: Mon Jul 10, 2006 3:42 am
by mattcooper
That's something I've tried already, with no joy! Thanka anyway... :)

Posted: Mon Jul 10, 2006 3:54 am
by GM
But astions is right: you are calling the create_Frame function before the parameters are being set...

Is anything at all being outputted? What's in the source code of the page?

Posted: Mon Jul 10, 2006 4:38 am
by mattcooper
The output in the page source show the table code, but the parameters are missing:

Code: Select all

<table border =  width =  height = >
this is after running the scripts with the changes astion suggested...

Re: Class variables not being used

Posted: Mon Jul 10, 2006 4:51 am
by GM
Maybe I'm missing something... Have you tried this or not?

Code: Select all

$mainclass=new mainClass; // Instantiate the main class
	$mainclass->LoadClass("subclass"); // Load the subclass
	
	$mainclass->border = "1";	
	$mainclass->width = "80%";	
	$mainclass->height = "200";	

	$mainclass->RunFunction('create_Frame');	
	$mainclass->RunFunction('create_Rows'); // Call a function defined in the subclass
	$mainclass->RunFunction('close_Table');
	
	$mainclass->RunFunction('two'); // this could create a button, insert an image, anything really!

Posted: Mon Jul 10, 2006 5:11 am
by mattcooper
Yes, I have tried that. It's exactly the same as the script I'm running at the moment!

Any thoughts?

Posted: Mon Jul 10, 2006 5:20 am
by GM
Now I understand...

I think it's a problem with the scope of the variables - you have defined $this->border etc. in the mainClass, but not in the subClass. From the subclass you are echoing "$this->border", where $this is refering to the subclass, not the mainclass.

EDIT: If you change the subclass class declaration to :

Code: Select all

Class subclass Extends mainClass {
...
}
the border, height and width attributes of the mainclass will become visible to the subclass.

Posted: Mon Jul 10, 2006 5:26 am
by mattcooper
Erm, hasn't made any difference! Doh...

Posted: Mon Jul 10, 2006 5:36 am
by JayBird

Code: Select all

$this->border
Is referencing a variable in the SubClass, which obviously doesn't exist in that scope, it exists in the parent class (mainClass).

Also, you may want to try using get_object_vars() to see what is going on in your classes.

Posted: Mon Jul 10, 2006 6:55 am
by Jenk
Instead of running a main class, and loading a subclass within, either scrap the main class altogether and just use the subclass, or extend the mainclass with the subclass and just use the one object.. or instead of creating a dynamic wrapper like that, create an interface. /me catches breath.