在静态函数中返回静态变量会在控制台中生成错误

I have been working on this for hours but not able to find the error. Also did searching on Internet but none seems fix to my weird problem. A bit weird as the same code works on different application that I have. The only thing differs is that I am now connecting to Sybase and the previous application connects to MySQL. Here is what I have in DB class:

class DB{
    private static $_instance = null;
    private function __construct(){
        try{
            $this->_pdo = new PDO ("dblib:host=host:port;dbname=myDb","username","pwd");
            echo 'Connected';
        }
        catch(PDOException $e){
            die($e->getMessage());
        }
    }

    public static function getInstance(){
        if(!isset( self::$_instance )){
            self::$_instance = new DB();
        }

        return self::$_instance; //this line generates 500 internal server error in browser console
    }
}

My code in index.php

echo DB::getInstance();

That outputs the word "Connected" but in the same time generates 500 code error in my browser's console. I don't really understand why return self::$_instance is having problem. When I do return 'test' no error generated. I know I miss something but I could not able to understand why. I am using PHP 5.3 on Ubuntu. Thank you in advance.

  1. Look at your error logs. You'll see a detailed error message there; the 500 error Apache is fronting is not meaningful here.
  2. Most likely the echo is the problem. DB::getInstance should return an object, and you cannot "echo" objects. The error you'll find in your error log is most likely "cannot convert object to string".