06-05-2009, 08:34 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: May 2009
Posts: 178
Thanks: 9
|
abstract classes and constructors
I've set up an abstract class to manage the collection of data from mysql tables and format them into tables before printing them out. As it stands the abstract class isnt doing much at present but i will be extending it with several implementation classes however all implementation classes with require input of a variable $username. Therefore I want to declare a constructor method to accept in username for every implementation class. Doesnt seem to work. I;ve tried moving the constructor down into the implementation class and still doesnt work always get the warning "Notice: Undefined variable: username" and it seems to run the query $query = "select username from login where (loggedin > 0) AND (username != '$username')"; ignoring the last section (username != '$username') however if i hardcode the value in instead of $username works fine. Am i doing something incorrect with classes or is this just a schoolboy syntax error i cant see. Any help greatly appreciated. Code below:
PHP Code:
abstract class TableWriter {
private $username;
/* Constructor */
private function __construct($username) {
$this->username = $username;
}
abstract public function writeTable();
}
class OnlineNowTable extends TableWriter {
public function writeTable() {
$query = "select username from login where (loggedin > 0) AND (username != '$username')";
$result = mysql_query($query) OR die('Cannot perform OnlineNowTable query!');
$str = "<table><tr><th>Online Now</th></tr>";
if (mysql_num_rows($result) == 0) {
$str .= "<tr><td>nobody online</td></tr>";
}
else {
while($row = mysql_fetch_array($result))
{
$str .= '<tr><td>'.$row['username'].'</tr></td>';
}
}
$str .= "</table>";
print $str;
}
}
|
|
|
|