dongyi0114 2017-03-27 16:29
浏览 76
已采纳

PHP静态属性中的运行时错误和访问类中的全局实例

I designed a PHP Class with a Static Properties and I get a blank page without any error log, Kindly assist me whats wrong in my PHP Code?

<?php

ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);

class Response
{
    public $Status;
    public $Message;

    function __construct() {
        $this->Status = FALSE;
        $this->Message = "";
    }        
}

$response = new Response();

class Connection
{
    static private $server = "localhost"
    static private $database = "Student";
    static private $user = "ram";
    static private $password = "ram12345!";

    static public $link;

    public static function Trigger() {
        try
        {
            if (!isset(self::$link)) {
                self::$link = mysql_connect("localhost", "bbminfoc_bbm", "Princess4BBM.>") or die("Couldn't make connection.");
                if(!self::$link)
                {
                    $response->Status = FALSE;
                    $response->Message = "Database Connection Failed !";
                }
                else
                {
                    if(!mysql_select_db(self::$database, self::$link))
                    {
                        $response->Status = FALSE;
                        $response->Message = "Couldn't select database Main !";
                    }
                }
            }
        }
        catch(Exception $e) {
            $response->Status = FALSE;
            $response->Message = "Exception: " . $e->getMessage();
        }

        if(!$response->Status)
        {
            unset(self::$link);
        }
    }
}

if(!isset(Connection::link))
{
    Connection::Trigger();
}

$outp = json_encode($response);

if(isset($outp))
{
    echo($outp);
}
else
{
    echo("No Data");
}

?>

Kindly assist me in this code, I don't know what I did wrong in the above pasted code because I can't able to see the log information in error_log file.

  • 写回答

4条回答 默认 最新

  • douwojiao5919 2017-03-27 16:33
    关注

    As @Yolo already mentioned in the comment, there's a semicolon missing in Connection::Trigger(); and at static private $server = "localhost". As this will cause a parse error the script will fail before executing and you won't see any errors.

    To see those errors you will have to find the php.ini configuration file and set display_errors = on. Also you should consider using an IDE with automatic code linting which will show you potential parse errors while typing the code.

    As taken from this answer.

    The completely fixed code looks like this:

    <?php
    
    ini_set("display_startup_errors", 1);
    ini_set("display_errors", 1);
    error_reporting(-1);
    
    class Response
    {
        public $Status;
        public $Message;
    
        function __construct() {
            $this->Status = FALSE;
            $this->Message = "";
        }        
    }
    
    $response = new Response();
    
    class Connection
    {
        static private $server = "localhost";
        static private $database = "Student";
        static private $user = "ram";
        static private $password = "ram12345!";
    
        static public $link = null;
    
        public static function Trigger() {
            global $response;
    
            try
            {
                if (self::$link !== null) {
                    self::$link = mysql_connect("localhost", "bbminfoc_bbm", "Princess4BBM.>") or die("Couldn't make connection.");
                    if(!self::$link)
                    {
                        $response->Status = FALSE;
                        $response->Message = "Database Connection Failed !";
                    }
                    else
                    {
                        if(!mysql_select_db(self::$database, self::$link))
                        {
                            $response->Status = FALSE;
                            $response->Message = "Couldn't select database Main !";
                        }
                    }
                }
            }
            catch(Exception $e) {
                $response->Status = FALSE;
                $response->Message = "Exception: " . $e->getMessage();
            }
    
            if(!$response->Status)
            {
                self::$link = null;
            }
        }
    }
    
    if(Connection::$link !== null)
    {
        Connection::Trigger();
    }
    
    $outp = json_encode($response);
    
    if(isset($outp))
    {
        echo($outp);
    }
    else
    {
        echo("No Data");
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作