dongyi0114 2017-03-27 08: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 08: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部