TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   abstract classes and constructors (http://www.talkphp.com/absolute-beginners/4484-abstract-classes-constructors.html)

captainmerton 06-05-2009 08:34 PM

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;
        
        }
    } 


Salathe 06-05-2009 08:51 PM

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.

captainmerton 06-06-2009 07:53 AM

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".

xenon 06-06-2009 08:43 AM

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.

captainmerton 06-06-2009 10:27 AM

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.

xenon 06-06-2009 05:10 PM

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.

captainmerton 06-07-2009 01:43 PM

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 06-08-2009 02:08 PM

Can anyone spot any bugs in the code above?

sketchMedia 06-08-2009 02:55 PM

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 


captainmerton 06-08-2009 06:36 PM

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.


All times are GMT. The time now is 04:59 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0