这个问题在这里已经有了答案:
Cannot find Class with PHP Namespace
(5 个回答)
去年关闭。
我收到一个 fatal error ,找不到类 THBS_AUTH_API,奇怪的是我包含了它(正确的文件)。
我试过 require、require_once、include_once 也是同样的问题。
错误:
Fatal error: Uncaught Error: Class 'THBS_AUTH_API' not found in /srv/disk1/3203065/www/thos-host.com/content/client_areas/thos-host-client-area/admin_actions/thbs_settings.php:111 Stack trace: #0 /srv/disk1/3203065/www/thos-host.com/client_area.php(1364): include() #1 {main} thrown in /srv/disk1/3203065/www/thos-host.com/content/client_areas/thos-host-client-area/admin_actions/thbs_settings.php on line 111
类(class):
/* some comments */
namespace THBS_API;
@include($_SERVER['DOCUMENT_ROOT']."/install/".$config_file_path);
class THBS_API
{
private const call_url = "https://YourWebsite.com/API/endsoftware.php";
private const API_USER = "YourAPIUser";
private const API_PASS = "YourAPIPass";
public static function CMD_EXTERNAL($CMD = null, $GET_STATUS = 0, $ADDITIONAL_PARAMETERS = null)
{
$base_url = $_SERVER['SERVER_NAME'];
$fields = array(
'API_USER' => urlencode(self::API_USER),
'API_PASS' => urlencode(self::API_PASS),
'CMD' => urlencode($CMD),
'FROM' => urlencode($base_url),
'ADDITIONAL_PARAMETERS' => urlencode($ADDITIONAL_PARAMETERS)
);
$fields_string = '';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, self::call_url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($result, true);
if($GET_STATUS == 0) { return $response['response_data']; }
else { return $response['response_status']; }
}
}
class THBS_AUTH_API
{
public static function CMD_AUTH($CMD = null, $GET_STATUS = 0, $ADDITIONAL_PARAMETERS = null)
{
$base_url = $_SERVER['SERVER_NAME'];
$fields = array(
'API_USER' => urlencode($API_USER),
'API_PASS' => urlencode($API_PASS),
'TOKEN' => urlencode($API_USER."|".$API_PASS),
'CMD' => urlencode($CMD),
'FROM' => urlencode("From https://website.com"),
'ADDITIONAL_PARAMETERS' => urlencode($ADDITIONAL_PARAMETERS)
);
$fields_string = '';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, "http://apiexample.com/API/private/THBS-API.php");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($result, true);
if($GET_STATUS == 0) { return $response['response_data']; }
else { return $response['response_status']; }
}
}
而且,我 所在的文件包括 这个类文件:
<?php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/API/clientapi.php")) {
include($_SERVER['DOCUMENT_ROOT']."/API/clientapi.php"); }
else { die('file not found (thbs-auth-api);'); }
?>
<div class="licensecard" id="license">License key: <?php echo THBS_AUTH_API::CMD_AUTH("LICENSE_KEY", 1); ?></div>
所以?
- 文件被正确包含。
- 我使用正确的函数来使用类;
为什么?为什么我收到这个错误?
最佳答案
您在命名空间 THBS_AUTH_API 下定义了您的类。
要使用此命名空间,请添加行 namespace THBS_AUTH_API;
在你打电话之前。
或改变
<div class="licensecard" id="license">License key: <?php echo THBS_AUTH_API::CMD_AUTH("LICENSE_KEY", 1); ?></div>
到
<div class="licensecard" id="license">License key: <?php echo THBS_AUTH_API\THBS_AUTH_API::CMD_AUTH("LICENSE_KEY", 1); ?></div>
更新
就像上面提到的评论一样,一个文件只能有一个命名空间。由于您删除了第二个“命名空间 THBS_AUTH_API;”在 thbs_settings.php 的顶部添加这一行:
namespace THBS_API;
或改变
<div class="licensecard" id="license">License key: <?php echo THBS_AUTH_API::CMD_AUTH("LICENSE_KEY", 1); ?></div>
到
<div class="licensecard" id="license">License key: <?php echo THBS_API\THBS_AUTH_API::CMD_AUTH("LICENSE_KEY", 1); ?></div>
关于php - 找不到类 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60136753/