TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 06-05-2009, 08:34 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default 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;
        
        }
    } 
captainmerton is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design