运行 PHP 静态属性中的时间错误和访问 Class 中的全局实例
Run time error in PHP Static Properties and Accessing Global Instance within a Class
我设计了一个 PHP Class
和 Static
属性,但我得到一个没有任何 error log
的空白页面,请帮助我 [=22] =]代码?
<?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 文件中看到日志信息。
正如@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");
}
?>
在 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
,请更改为 PDO
or mysqli
将其更改为:
if(!isset(Connection::link))
{
Connection::Trigger();
}
这个:
if(!isset(Connection::$link))
{
Connection::Trigger();
}
你不需要取消连接它会自动取消..在class,
之后
你在 localhost 后错过了分号,你没有在函数触发器中调用响应...
<?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");
}
我设计了一个 PHP Class
和 Static
属性,但我得到一个没有任何 error log
的空白页面,请帮助我 [=22] =]代码?
<?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 文件中看到日志信息。
正如@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");
}
?>
在 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
,请更改为 PDO
or mysqli
将其更改为:
if(!isset(Connection::link))
{
Connection::Trigger();
}
这个:
if(!isset(Connection::$link))
{
Connection::Trigger();
}
你不需要取消连接它会自动取消..在class,
之后你在 localhost 后错过了分号,你没有在函数触发器中调用响应...
<?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");
}