【问题标题】:Class not found (PHP) [duplicate]找不到类(PHP)[重复]
【发布时间】:2020-05-24 23:10:48
【问题描述】:

我收到一个致命错误,找不到类 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

类:

/* 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>

所以? - 正确包含文件。 - 我使用正确的函数来使用类;

为什么?为什么会出现这个错误?

【问题讨论】:

  • 那个 one 文件是否有 two 命名空间?因为根据the manual:“在命名空间声明之前允许的唯一代码结构是声明语句,用于定义源文件的编码。
  • 一个文件中不能有 2 个命名空间。
  • 呃,等等,我删除一个
  • 完成,现在我得到同样的错误。

标签: php class namespaces include


【解决方案1】:

您在命名空间 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>

更新 就像上面提到的 cmets 一样,一个文件只能有一个命名空间。由于您删除了第二个“命名空间 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>

【讨论】:

  • Parse error: syntax error, unexpected 'use' (T_USE) in /srv/disk1/3203065/www/thos-host.com/content/client_areas/thos-host-client-area/admin_actions/thbs_settings.php on line 3
  • 截图代码:prnt.sc/qzpvta
  • 删除了一个命名空间,因为我不能在同一个文件中使用 2
  • @Thos-Host 我测试了您的代码,它适用于上述一项或其他更改。 :)
  • 我删除了命名空间,现在我的问题很正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2016-09-07
  • 2018-06-20
  • 1970-01-01
  • 2018-07-02
  • 2021-11-30
  • 2020-07-17
相关资源
最近更新 更多