View Single Post
Old 06-01-2009, 06:34 PM   #1 (permalink)
captainmerton
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Using static method to create objects

Wonder if anyone can help me with the syntax for this. I have created a class called Collective and with that class a public static method called getInstance which reads several rows from a mysql table and creates an object of class Collective for every row read. I;m not getting any major syntax errors when i run this but it just isnt working well the db call is working and returning what i expect but the object creation but isnt. Any advice? (PS - I'm just passing in joebloggs to test the class and i'm getting rows back from the database so no problem there).

Client Code:
PHP Code:
require_once('./lib/collective.class.php');
$coltable Collective::getInstance('joebloggs'); 
Collective Class code:
PHP Code:
    class Collective {
        
/* Custom Error Message for a field left blank */
        
const ERROR_NO_COLLECTIVES "Error - No Collectives Found!";

        
/* The collective id */
        
private $colid;
        
        
/* The collective name */
        
private $colname;

        
/* The collective name */
        
private $username;

        
/* Instance of Object */
        
private static $instance;


        
/* Contructor */
        
private function _contruct($colid$colname) { 
        
$this->colid    $colid;
        
$this->colname    $colname;
        
$this->dispColid();
        
$this->dispColname();    
        }
    
        
/* Create Object Instances */
        
public static function getInstance($username) {
            
$query "select colid, colname from collective where username = '$username'";

            
$result mysql_query($query) OR die('Cannot perform getInstance query!');    

                if (
mysql_num_rows($result) == 0) {        
                    throw new 
Exception(Collective::ERROR_NO_COLLECTIVES);
                }
                else {
                    while(
$row mysql_fetch_array($result))
                    {
                    
self::$instance = new Collective($row['colid'],$row['colname']);
                    }
                }
        }
    
        
/* Return the collective id */
        
public function getColid() {
            return 
$this->colid;
        }
    
        
/* Return the collective name */
        
public function getColname() {
            return 
$this->colname;
        }

        
/* Return username associated with the collective */
        
public function getUsername() {
            return 
$this->username;
        }

        
/* Print the collective id */
        
public function dispColid() {
            print 
$this->colid;
        }
    
        
/* Print the collective name */
        
public function dispColname() {
            print 
$this->colname;
        }
            
    } 
captainmerton is offline  
Reply With Quote