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-01-2009, 06:34 PM   #1 (permalink)
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
Old 06-03-2009, 02:25 AM   #2 (permalink)
The Wanderer
 
Join Date: Oct 2008
Location: Brazil, Balneário Camboriu
Posts: 6
Thanks: 1
gilbertoalbino is on a distinguished road
Default Review any typo!

Hi, dear Captain Merton!

I've run some tests on your script and have little to say!

Your code is okay, the SingleTon Pattern is correct,
but you typed the Constructor wrong:

Code:
private function _contruct($colid, $colname)
The correct is:

Code:
private function __construct($colid, $colname)
Just it! Congratulation on your way with Design Patterns!
__________________
...it's about producing and sharing!
Send a message via MSN to gilbertoalbino
gilbertoalbino is offline  
Reply With Quote
Old 06-03-2009, 08:27 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Brilliant thanks Gilberto. In all my testing I could tell that it was not getting to the construct function but was throwing no error as i assume it was simply treating it as another class method. But I didnt ever consider the spelling being wrong. Thanks for taking the time to look it over. Cheers.
captainmerton is offline  
Reply With Quote
Old 06-03-2009, 01:30 PM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2008
Location: Brazil, Balneário Camboriu
Posts: 6
Thanks: 1
gilbertoalbino is on a distinguished road
Default

You're welcome!
Well, I did every possible test and, in fact, finding the spelling error was the last thing I did!
__________________
...it's about producing and sharing!
Send a message via MSN to gilbertoalbino
gilbertoalbino 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
Create your own advanced WYSIWYG editor almsamim Javascript, AJAX, E4X 8 10-23-2008 06:41 PM
Assigning non-static properties from a static method? delayedinsanity Advanced PHP Programming 4 07-10-2008 02:49 PM
Need your feedback Tanax Absolute Beginners 29 10-11-2007 04:50 PM


All times are GMT. The time now is 12:12 PM.

 
     

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