2017-03-27 42 views
-2

我設計了一個具有Static屬性的PHP Class,並且我沒有任何error log的空白頁,請幫助我在我的PHP代碼中發生什麼錯誤?PHP靜態屬性中的運行時錯誤和類中的訪問全局實例

<?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"); 
} 

?> 

請幫助我這個代碼,我不知道我做錯了什麼在上面粘貼代碼,因爲我不能夠看到的error_log文件中的日誌信息。

+0

嘗試增加分號連接後'::觸發();' – Yolo

+0

你_really_肯定的是,你_not_獲得入門在你的http服務器錯誤日誌文件中?如果你在你的代碼中有這樣一個明確的語法錯誤,那將意味着你已經切換了錯誤日誌,這是你應該不應該做的事情...... – arkascha

+0

'json_encode($ response);'?你想要json_encode一個對象嗎? –

回答

1

由於@Yolo在評論中已經提到,在Connection::Trigger();static private $server = "localhost"處缺少一個分號。因爲這會導致分析錯誤,所以腳本在執行之前會失敗,並且不會看到任何錯誤。

要查看這些錯誤,您將必須找到php.ini配置文件並設置 display_errors = on。你也應該考慮使用帶有自動代碼分割的IDE,這會在你輸入代碼的時候顯示出潛在的分析錯誤。

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"); 
} 

?> 
+0

是的,我嘗試了,我仍然得到一個空白頁面... –

+0

即使使用display_errors = on,一些錯誤也不會顯示,因爲它會顯示給stderr。你可以從bash shell運行它嗎? – Forbs

+0

*「從這個答案中獲得。」* - 他們已經在使用它。 –

1

static private $server = "localhost"你錯過了最後;

而且,我得到這個錯誤:
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in ... on line 61

所以,改變if(!isset(Connection::link))if (Connection::$link !== null)

這應該可以解決你的問題。

注意
請不要使用mysql_*擴展。它已被棄用。如果你想繼續使用mysql,更改爲PDOmysqli

0

更改爲:

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

此:

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

ü不需要以未設置連接,它會自動將未上課..課後,

ü錯過本地主機後分號,U沒有調用函數觸發器內響應...

<?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 = "listings"; 
     static private $user = "root"; 
     static private $password = ""; 

     static public $link; 

     public static function Trigger() { 

      global $response; 
      try 
      { 
       if (!isset(self::$link)) { 
        self::$link = mysql_connect("localhost", "root", "") 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(!isset(Connection::$link)) 
    { 
     Connection::Trigger(); 
    } 

    $outp = json_encode($response); 

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

     echo "hello"; 
     echo("No Data"); 
    }