无法从其他命名空间加载供应商类 [英] Unable to load vendor classes from other namespace

查看:47
本文介绍了无法从其他命名空间加载供应商类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在 PHP 中环绕命名空间,尤其是当您的代码需要与驻留在另一个命名空间中的脚本进行交互时.我下载了一个 Shopify API 工具包并试图让它工作.在我开始向我的代码添加命名空间之前,一切都很好(这是必需的,或者与我网站上的其他 Wordpress 插件发生脚本冲突).此外,顶部奇怪的命名空间 {} 位是因为在同一个文件中,我想要一个全局可访问的函数来使类成为单例.

I'm having some hard time wrapping around namespaces in PHP, especially when you code needs to interact with scripts residing in another namespace. I downloaded a Shopify API toolkit and trying to get it working. Everything was fine before I started adding namespaces to my code (which is required or threre is script collisition with other Wordpress plugins on my site). Also, the weird namespace {} bit at the top is because in this same file I want a globally accessible function for making the class a singleton.

期待更多地了解其工作原理.

Looking forward to learning more about how this works.

#### FILE BEING CALLED

namespace {

    function SomeFunctionToBeAccessedGlobally() {
        return 'Hello';
    }
}

namespace MySpecialApp {

    class ShopifyImport {

        public function __construct() {
            // Do Whatever
            $this->doImport();
        }

        public function doImport() {

            require __DIR__ . '/vendor/autoload.php';

            $credential     = new Shopify\PrivateAppCredential('standard_api_key', 'secret_api_key', 'shared_api_key');
            $client         = new Shopify\Client($credential, 'shop_url', [ 'metaCacheDir' => './tmp' ]);

        }

    }

}


#### FILE '/vendor/autoload.php'

require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit73503f8de5d68cdd40a9c0dfd8a25b44::getLoader();

我确实注意到部分存储库克隆到 vendor 的某些文件具有 namespace Slince\Shopify; 声明.我尝试在原始命名空间中对该命名空间执行 use 操作,但它也不起作用.

I do noice that some of the files which where part of the repository cloned into vendor have namespace Slince\Shopify; declarations. I tried to do a use with that namespace within my original namespace but it also didn't work.

报告的 PHP 错误是:

致命错误:未捕获的错误:类在中找不到MySpecialApp\Shopify\PrivateAppCredential"//.../ShopifyImporter.php:139 堆栈跟踪:#0 (Blah Blah Blah)

Fatal error: Uncaught Error: Class 'MySpecialApp\Shopify\PrivateAppCredential' not found in /.../ShopifyImporter.php:139 Stack trace: #0 (Blah Blah Blah)

推荐答案

您的代码尝试在当前命名空间中创建一个新的 Shopify\PrivateAppCredential() 对象.但是,此类在您的命名空间中不存在,因为它是供应商"命名空间的一部分.

Your code tries to create a new Shopify\PrivateAppCredential() object in the current namespace. However, this class does not exist in your namespace since it is part of the "vendor" namespace.

您可以通过在对象创建之前添加 \ 来重置"(读取回退)到全局命名空间,如文档:

You can "reset" (read fallback) to the global namespace for your object creations by adding a \ in front of them, as described in the documentation:

$credential = new \Shopify\PrivateAppCredential('standard_api_key', 'secret_api_key', 'shared_api_key');

<小时>

您可以在此处查看差异 没有 \使用 \.

这篇关于无法从其他命名空间加载供应商类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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