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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 06-05-2009, 08:51 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Two minor issues. Firstly, You're just using $username in writeTable, a local variable which is not defined anywhere. Secondly, $username is declared as private in the TableWriter abstract class, so child classes (OnlineNowTable cannot make use of it.
Salathe is offline  
Reply With Quote
Old 06-06-2009, 07:53 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Hi thanks for getting back to me. I've declared $username as public in TableWriter but not sure what you mean "You're just using $username in writeTable, a local variable which is not defined anywhere" - i still cant get this to work. Something about the way i am declaring and trying to use $username. Even if i try and print it out in the construct method after i've assigned it a value i still always get "Notice: Undefined variable: username".
captainmerton is offline  
Reply With Quote
Old 06-06-2009, 08:43 AM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

What Salathe was saying is that you have to use $this->username instead of $username. The first one is a class member, the latter is just a local variable. Then, $this->username does not have to be public, it can be protected. That way, it has kind of "private" visibility which is also inherited to children, but cannot be overwritten from outside the class.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 06-06-2009, 10:27 AM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Sorry I still dont understand. Apologies i'm a beginner. have tried everything and still doesnt work. Could you correct the code for me and repost and then maybe i'll understand. PS - appreciate the help. Cheers.
captainmerton is offline  
Reply With Quote
Old 06-06-2009, 05:10 PM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

PHP Code:
class OnlineNowTable extends TableWriter {

    public function 
writeTable() {
        
$query "select username from login where (loggedin > 0) AND (username != '$this->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;
    
    }

You don't have $username = 'something' somewhere in the writeTable method, therefore $username is out of scope (meaning it doesn't exist in that method -- function). You want to use the class property $username, which is available through $this->username.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 06-07-2009, 01:43 PM   #7 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Apologies still cant get this to work. Any advice? I am now getting the message "Fatal error: Using $this when not in object context" when i use $this->username

Here's the onject instantiation client code:

PHP Code:
OnlineNowTable::writeTable($_SESSION['name']); 
Here's the class code:

PHP Code:
    abstract class TableWriter {
        public 
$username;

        
/* Constructor */
        
public 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 != '$this->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
Old 06-08-2009, 02:08 PM   #8 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Can anyone spot any bugs in the code above?
captainmerton is offline  
Reply With Quote
Old 06-08-2009, 02:55 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

That is because you have not instantiated the object, therefore $this will never contain the reference to the calling object (as it doesn't exist) instead you are using a static call to a class method (which in your case isn't declared static which is bad practice).

You would need something like this:
PHP Code:
$tblWriter = new OnlineNowTable(); //create a new object
$tblWriter->writeTable($_SESSION['name']); //run the writeTable method 
But looking at your code, I think you need it like this:
PHP Code:
$tblWriter = new OnlineNowTable($_SESSION['name']); //create a new object
$tblWriter->writeTable(); //run the writeTable method 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
captainmerton (06-08-2009)
Old 06-08-2009, 06:36 PM   #10 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Now works fine. Great thanks for your help. I had no intention of using a static method i just forgot to instantiate the object pretty basic mistake.
captainmerton is offline  
Reply With Quote
Reply



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 02:33 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