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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧