Page 1 of 1

Problem with accessing static variable

Posted: Mon Jun 07, 2010 8:53 am
by hatboy36
Here's my code:

Code: Select all

<?

include_once "./Database.class.php";
include_once "./defined.php";

/*****************************************************************************************************************
* Name: DbConnection
*
* Purpose: Class that manages single instantiation of a Database object (see Database.class.php)
*
****************************************************************************************************************/
class DbConnection {

    private static $connnection;
   
    /***********************************************************************************************
     *
     *  Name: init
     *
     *  Purpose: Instatiates a connection to the database
     *
     *  Parameters: none
     *
     *  Returns: none
     *
     ************************************************************************************************/
    public static function init() {
        self::$connection = new Database(SERVER, USERNAME, PASSWORD, DBNAME);
    }
   
    /***********************************************************************************************
     *
     *  Name: query
     *
     *  Purpose: Handles queries to the database. This function will connect to the database run the specified query
     *  disconnect from the database and return the results as an array or a null on an error.
     *
     *  Parameters:
     *     sql - the sql string used to query the database
     *
     *  Returns: the result set, as an array, that resulted from running the query.
     *
     ************************************************************************************************/
    public static function query($sql) {
        return self::$connection->query($sql);
    }
   
    /***********************************************************************************************************
     * Name: execute
     *
     * Purpose: Handles the execution of sql on the database. Types of sql that should be sent here include INSERT,
     * UPDATE, or anything that shouldn't return a result set.
     *
     * Incoming Parameters:
    *       sql - the sql string to execute on the database
     *
     * Returns: nothing
     *
     *************************************************************************************************************/
    public static function execute($sql) {
        self::$connection->execute($sql);
    }
}
?>
But if I make the following call:

Code: Select all

DbConnection::init();
I get the following fatal error:

Code: Select all

Fatal error: Access to undeclared static property: DbConnection::$connection in /home/user/public_html/DbConnection.class.php  on line 42
What is causing this fatal error? Thanks for your help.

Re: Problem with accessing static variable

Posted: Mon Jun 07, 2010 9:15 am
by cpetercarter
Spelling error - you have misspelled $connection as $connnection.