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 subclassCode: 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!Thanks in advance!