当函数被调用两次时,PHP不能重新声明对象


PHP Can't redeclare object in function when called twice

我正在制作一个PHP API,并且正在调用一个函数来从我们的DBHandler中提取请求。当我得到'/与会者'功能时,我正确地提取数据。但我想添加身份验证到每个调用api。我们将一个参数传递给DBHandler,以指定要使用哪个连接字符串(因为我们有多个)。如果字符串被定义为null,它将使用标准连接字符串。

'authentication'函数需要使用标准连接字符串,而'/participants ' get需要一个不同的连接字符串。如上所述,'/与会者'本身工作得很好,但当我添加验证时,它会在尝试执行sql时出错。我知道这是因为身份验证使用了不同的连接字符串,并且以某种方式覆盖了连接。

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
'Slim'Slim::registerAutoloader();
$app = new 'Slim'Slim();
$user_id = NULL;
 function authenticate('Slim'Route $route) {
     $headers = apache_request_headers();
     $response = array();
     $app = 'Slim'Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         $db = new DbHandler($dm_config);
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }
$app->get('/attendees','authenticate', function() {
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            $db = new DbHandler($dm_config);
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });

我如何得到函数调用相同的对象,但单独使用?

您在两个函数中传递相同的连接字符串$dm_config

您应该为每个连接使用不同的变量

您可以声明static $db变量初始化为null,并在连接db之前使用empty()函数检查它是否为空。

function authenticate('Slim'Route $route) {
     static $db=null;
     $headers = apache_request_headers();
     $response = array();
     $app = 'Slim'Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(empty($db))
         {
             $db = new DbHandler($dm_config);
         }
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

或者您可以创建全局变量$db,并在两个函数中通过global关键字访问它,如下所述

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
'Slim'Slim::registerAutoloader();
$app = new 'Slim'Slim();
$user_id = NULL;
$db=null; //global bariable
 function authenticate('Slim'Route $route) {
     global $db; // access global variable
     $headers = apache_request_headers();
     $response = array();
     $app = 'Slim'Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(!empty($db)) {$db = new DbHandler($dm_config)};
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }
$app->get('/attendees','authenticate', function() {
            global $db; // access global variable
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            if(!empty($db)) {$db = new DbHandler($dm_config)};
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });