(1/1) FatalErrorException 无法声明比特币类,因为该名称已在 easybitcoin.php 第 4 行中使用

Posted

技术标签:

【中文标题】(1/1) FatalErrorException 无法声明比特币类,因为该名称已在 easybitcoin.php 第 4 行中使用【英文标题】:(1/1) FatalErrorException Cannot declare class Bitcoin, because the name is already in use in easybitcoin.php line 4 【发布时间】:2019-07-09 14:00:51 【问题描述】:

我有 2 个文件都同名 easybitcoin.phpEasybitcoin.php

这是它抛出的错误

(1/1) 致命错误异常

无法声明比特币类,因为该名称已在 easybitcoin.php 第 4 行

第 4 行是

class Bitcoin 

如果有人能指出我正确的方向。每次我去点击生成新的钱包地址。 Laravel 抛出这个错误

<?php 

class Bitcoin 
    // Configuration options
    private $username = ;
    private $password;
    private $proto;
    private $host;
    private $port;
    private $url;
    private $CACertificate;
    // Information and debugging
    public $status;
    public $error;
    public $raw_response;
    public $response;
    private $id = 0;
    /**
     * @param string $username
     * @param string $password
     * @param string $host
     * @param int $port
     * @param string $proto
     * @param string $url
     */
    function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null) 
        $this->username      = $username;
        $this->password      = $password;
        $this->host          = $host;
        $this->port          = $port;
        $this->url           = $url;
        // Set some defaults
        $this->proto         = 'http';
        $this->CACertificate = null;
    
    /**
     * @param string|null $certificate
     */
    function setSSL($certificate = null) 
        $this->proto         = 'https'; // force HTTPS
        $this->CACertificate = $certificate;
    
    function __call($method, $params) 
        $this->status       = null;
        $this->error        = null;
        $this->raw_response = null;
        $this->response     = null;
        // If no parameters are passed, this will be an empty array
        $params = array_values($params);
        // The ID should be unique for each call
        $this->id++;
        // Build the request, it's ok that params might have any empty array
        $request = json_encode(array(
            'method' => $method,
            'params' => $params,
            'id'     => $this->id
        ));
        // Build the cURL session
        $curl    = curl_init("$this->proto://$this->host:$this->port/$this->url");
        $options = array(
            CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
            CURLOPT_USERPWD        => $this->username . ':' . $this->password,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_FOLLOWLOCATION => TRUE,
            CURLOPT_MAXREDIRS      => 10,
            CURLOPT_HTTPHEADER     => array('Content-type: application/json'),
            CURLOPT_POST           => TRUE,
            CURLOPT_POSTFIELDS     => $request
        );
        // This prevents users from getting the following warning when open_basedir is set:
        // Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
        if (ini_get('open_basedir')) 
            unset($options[CURLOPT_FOLLOWLOCATION]);
        
        if ($this->proto == 'https') 
            // If the CA Certificate was specified we change CURL to look for it
            if ($this->CACertificate != null) 
                $options[CURLOPT_CAINFO] = $this->CACertificate;
                $options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate);
            
            else 
                // If not we need to assume the SSL cannot be verified so we set this flag to FALSE to allow the connection
                $options[CURLOPT_SSL_VERIFYPEER] = FALSE;
            
        
        curl_setopt_array($curl, $options);
        // Execute the request and decode to an array
        $this->raw_response = curl_exec($curl);
        $this->response     = json_decode($this->raw_response, TRUE);
        // If the status is not 200, something is wrong
        $this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        // If there was no error, this will be an empty string
        $curl_error = curl_error($curl);
        curl_close($curl);
        if (!empty($curl_error)) 
            $this->error = $curl_error;
        
        if ($this->response['error']) 
            // If bitcoind returned an error, put that in $this->error
            $this->error = $this->response['error']['message'];
        
        elseif ($this->status != 200) 
            // If bitcoind didn't return a nice error message, we need to make our own
            switch ($this->status) 
                case 400:
                    $this->error = 'HTTP_BAD_REQUEST';
                    break;
                case 401:
                    $this->error = 'HTTP_UNAUTHORIZED';
                    break;
                case 403:
                    $this->error = 'HTTP_FORBIDDEN';
                    break;
                case 404:
                    $this->error = 'HTTP_NOT_FOUND';
                    break;
            
        
        if ($this->error) 
            return FALSE;
        
        return $this->response['result'];
    

【问题讨论】:

两个文件是否都定义了Bitcoin 类?请显示完整的错误。 【参考方案1】:

您正在使用两个具有相同名称的类,或者您包含两次相同的类。 假设问题是第一个,您可以为每个文件分配一个命名空间。 然后,如果你需要将第二个类调用到第一个类中,你可以给它一个别名并通过这个来调用它。

file1.php:

namespace App\Something;

class Bitcoin 
    public static doSomething()

file2.php:

namespace App\YouCoolPath;

use App\Something\Bitcoin as YourAlias;

class Bitcoin 
    public function __construct() 
        YourAlias::doSomething();
    


【讨论】:

以上是关于(1/1) FatalErrorException 无法声明比特币类,因为该名称已在 easybitcoin.php 第 4 行中使用的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 4刀片模板导致FatalErrorException?

HtmlServiceProvider.php 第 36 行中的 FatalErrorException:laravel

Inflector.php 第 265 行中的 FatalErrorException:语法错误,意外 ':',期待 ';'要么 ''

Laravel 5.4 上 Manager.php 第 139 行中的 FatalErrorException

php artisan 抛出 FatalErrorException 类未找到

我得到了 FatalErrorException: 试图在奏鸣曲管理员中创建自定义操作