在类中使用第3方api时发生命名空间冲突 [英] Namespace conflict when using 3rd party api in class

查看:67
本文介绍了在类中使用第3方api时发生命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了一些奇怪的事情.我是关于php编程的新手. 我正在尝试使用FirePHP,但实际上我的问题与该工具没有直接关系.

There is something weird going on. I'm a novice with respect to php programming. I'm trying to use FirePHP but my question is actually not related to that tool directly.

您将在下面看到的函数fb是FirePHP的一部分.我的入口是Main.php.执行第一个fb调用没有任何问题,但是第二个调用(请参阅ExperController.php)(在调用Main.php中的$ec->exper();时触发)会导致致命错误:

The function fb you'll see below is part of FirePHP. My entry point is Main.php. The first fb call is executed without any problems but the second call (see ExperController.php) ,which gets triggered when $ec->exper(); in Main.php is called, causes a fatal error:

致命错误:未捕获的错误:在第12行的wp目录\ wp-content \ mu-plugins \ typerocket \ app \ Controllers \ ExperController.php路径中找不到类'App \ Controllers \ FirePHP'

运行时引擎为什么要在该命名空间(App \ Controllers)下查找类FirePHP?为什么在第一次fb调用期间这不是问题? FirePHP文件中根本没有定义任何名称空间.我的最后一个但至关重要的问题是,如何在不接触第3方文件的情况下解决此问题? 此答案不是我的情况的解决方案,因为我我没有在我的代码中引用/调用提到的类!

Why is the runtime engine looking for the class FirePHP under that namespace (App\Controllers)? And why wasn't this an issue during the first fb call? There isn't any namespace defined in the FirePHP files at all. And my last but crucial question, how can I fix this without having to touch 3rd party files? This answer is not a solution for my case since I'm not referencing / calling the mentioned class in my code!

注意:FirePHP是通过自动加载器提供的(由composer提供).

Note: FirePHP is included via autoloader (provided by composer).

Main.php:

<?php

fb('Hello World!', FirePHP::INFO);

$req = new \TypeRocket\Http\Request();
$res = new \TypeRocket\Http\Response();
$ec = new \App\Controllers\ExperController($req, $res);
$ec->exper();

ExperController.php :

<?php

namespace App\Controllers;


use TypeRocket\Controllers\Controller;

class ExperController extends Controller
{

    public function exper() {
        fb('Hello World!', FirePHP::INFO);
    }

}

推荐答案

您是否使用像composer这样的任何依赖项管理器? 当您从主要方式以某种方式调用它时,php可以看到FirePHP类在哪里,并且一切正常,但是当您尝试在另一个文件夹中的类内访问它时,则必须定义该类在控制器内的位置,例如如何定义控制器的位置您扩展的类:

do you use any Dependency manager like composer?? when you call it from main somehow php can see where is FirePHP class is and everything works fine but when you try to access it inside a class in another folder you must define where the class is inside your controller , like how you defined where is controller class that you extend :

use TypeRocket\Controllers\Controller;

因此将FirePHP类添加到您的控制器中,然后一切正常.

so add FirePHP class to your controller and everything will be ok then.

use Path\To\Class\FirePHP;

如果您已完成所有操作但仍无法正常工作,则可以尝试转储自动加载;

also if you did all and still not working you may try dump autoload;

composer dump-autoload

这篇关于在类中使用第3方api时发生命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆