Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fatal error:

Status
Not open for further replies.

waydown

Programmer
Apr 27, 2009
49
GB
Hi,
I have something like shown below:

class Anon
{
$_dddd = null;
.......
public function _construct()
{ $this->_dddd = DB::getInstance();
}
$check = $this->_dddd->get(.......)
.......
}

$_dddd is holding a SINGLETON object.
When I do above I get following message:
Fatal error: Call to a member function get() on a non-object

Could someone point out the mistake in my code and how to rectify it. Will be very grateful.
 
DB::getInstance() is not returning an instance of a class.
shows us some more code (the DB class for example) and we may be able to assist.
 
Hi,
Thanks for your reply. Here is the relevant part of my DB class
class DB
{
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;

private function _construct()
{ try
{ this->_pdo = new PDO('mysql:host='.Config::get

('mysql/host'); 'mysql:database='.Config::get('mysql/database'),

Config::get('mysql/username'), Config::get('mysql/password'));
}
catch(PDOException $excp)
{ die($excp->getMessage());
}
}
public static function getInstance()
{ if(!isset(self::$_instance))
{ self::$_instance = new DB();
}
return self::$_instance;
}
 
__construct() (with double underscore) is part of your problem.

check out Link


 
please remember always to post code between [ignore]
Code:
[/ignore] tags. very difficult to read otherwise.

bunch of errors there.

the 'this' keyword must be a variable ($this) in php.
the mysql dsn is wrong set out, with the semi-colon in the wrong place and the keywords incorrect.

Code:
<?php
class DB{
    private static $_instance = null;
    private $_pdo, $_query, $_error = false, $_results, $_count = 0;

    private function _construct(){ 
        try{ 
            $dsn = sprintf(         "mysql:host=%s;dbname=%s;", 
                                    Config::get('mysql/host'), 
                                    Config::get('mysql/database')
                            );
            $this->_pdo = new PDO(  $dsn, 
                                    Config::get('mysql/username'), 
                                    Config::get('mysql/password')
                                );
        } catch(PDOException $excp){ 
            die($excp->getMessage());
        }
    }

    public static function getInstance(){ 
        if(!(self::$_instance)):
            self::$_instance = new self(); 
        endif;
        return self::$_instance;
    }
}
$pdo = DB::getinstance();
var_dump($pdo);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top