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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里