Results 1 to 5 of 5

Thread: Singleton with PHP

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Singleton with PHP

    I have the following class:

    Code:
    <?php
    
    // this is a singelton database connect class
    
    class dbconnect {
    
    
        // Store the single instance of Database
        private static $m_pInstance;
    
        private function __construct() {
    
            	// *** Database Connect ***
    			$dblink = mysql_connect('localhost', 'root', 'pwd') OR die(mysql_error());
    			$m_pInstance = mysql_select_db(",ydb") or die(mysql_error());
    			
    
        }
    
    
        private function __clone() {
    
    
                
    
        }
    
    
    
    
        public static function getInstance()
        {
            if (!self::$m_pInstance)
            {
                self::$m_pInstance = new dbconnect();
            }
    
            return self::$m_pInstance;
        }
    
    
    
    
    
    }
    
    
    
    ?>
    but I get the following error when I have muliple include statements using this class:

    Fatal error: Cannot redeclare class dbconnect in


    I know I need some code in the __Clone function. Im not sure what to put in there, can someone help?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Singleton with PHP

    A singleton is a class which can only have a single instance. Cloning the object would create multiple instances and it would therefore it would no longer be a singleton.

    Why do you need to have multiple instances of the class?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Singleton with PHP

    I see your point, maybe I am doing something wrong, but check out why its erroring.

    I have one class, lets call it class A. I also have another class, lets call it class B.

    Both of these classes "require 'dbconnect.php';" since they both need to connect to the database.

    Now in my index.php file, I want to make instances of both A and B classes but both "require 'dbconnect.php';".

    See the problem? There is no error when I either use class A alone or class B alone in index.php. It only errors when I use both at the same time.

    Thanks for helping out.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Singleton with PHP

    Use require_once instead. It will ensure the class is only declared once; even if the file is included more than once.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Singleton with PHP

    It works, thanks boss.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width