douwo8140 2014-12-30 04:34
浏览 27
已采纳

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

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条回答 默认 最新

  • douyun3887 2014-12-30 04:36
    关注
    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".
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么