尝试使用特征但一直说未找到

trying to use a trait but keeps saying not found

我有一个连接到外部端点的 api 特性。我想在名为 ProductClass 的 class 中使用此特征。该特征与 class 位于同一文件夹中,但我收到错误消息是我在 class 中添加了 use ApiTrait。错误说它找不到特征,所以如果我在 class 文件的顶部包含特征文件,我会得到这个错误,找不到 ApiTrait ProductClass\ApiTrait。 如果我将特征传递给构造函数,当我调用 ProductClass 时,我的索引页面会出现错误,因为我没有传递特征。我不想将任何参数传递给构造函数,只是将字符串顶部附加到 .env 端点。非常感谢任何线索 这是我的 ApiTrait 代码

<?php
namespace ApiTrait;

require './vendor/autoload.php';
use GuzzleHttp\Client;

trait ApiTrait
{
    protected $url;
    protected $client;

  public function __construct()
  {
    $this->url =  getenv('API_URL');
    $this->client = new Client();

  }

  private function getResponse(String $uri = null)
  {
    $full_path = $this->url;
    $full_path .=$uri;
    try {
        $response = $this->client->get($full_path);    
    }
    catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
    }
    return json_decode($response->getBody()->getContents(), true);

  }
  public function getAPIData($uri)
  {
    return $this->getResponse($uri);
  }
}

这是我的 ProductClass 代码

<?php

namespace ProductClass;
include_once("ApiTrait.php");

use DataInterface\DataInterface;

class Product implements DataInterface
{ 
    use ApiTrait\ApiTrait
    private $api;

    public function __construct(ApiTrait\ApiTrait $apiTrait) {
       $this->api = $apiTrait;
    }

    private function getResponse($append, $try) {
        $urlAppend = $append; 
        $good_data = false;

        do{
          try{
            $result = $this->api->getAPIData($urlAppend);
            //check data to see if valid
            if(!array_key_exists( "error",$result)){
                $good_data = true;
                return $result;
            }
          }
          catch(Exception $e){
            //call api upto 10 times
            if($try < 10) {
                sleep(1);
                getData($append, $try++);
            } else { //return a connection  error
                $api_error['error']='unable to connect to api';
                return  $api_error;
            }
        }
      } while($good_data === false);
  }

  public function getData($append, $try = 0)
  {
     return $this->getResponse($append, $try);
  }

}

如果您使用的是自动加载器,则永远不需要:

include_once("ApiTrait.php");

您已经在 ApiTrait 命名空间中定义了您的特征:

namespace ApiTrait;
trait ApiTrait { ... }

即特征的完整路径是\ApiTrait\ApiTrait。如果您在定义它的命名空间之外的命名空间中使用该特征,那么在引用它时需要从根命名空间锚定,方法是在它前面加上反斜杠:

namespace ProductClass;
class Product implements DataInterface
{ 
    use \ApiTrait\ApiTrait;

否则,如果您 use ApiTrait\ApiTrait; 没有前导反斜杠,那么 PHP 认为您指的是当前命名空间,即 ProductClass,产生 \ProductClass\ApiTrait\ApiTrait -- 不存在,因此你的错误。

你也可以用 class 别名这样做:

namespace ProductClass;
use ApiTrait\ApiTrait;
class Product implements DataInterface
{ 
    use ApiTrait;

此外,您似乎只是将每个 class 都放在了自己的命名空间中。不要那样做。使用命名空间对常用项进行分组,例如,像这样:

namespace Traits;
trait Api { ... }

namespace Traits;
trait Foo { ... }

namespace Traits;
trait Bar { ... }

namespace App;
class Product {
    use \Traits\Api;
    use \Traits\Foo;
    use \Traits\Bar;
}