ZF2:自动加载没有命名空间的库 [英] ZF2: autoloading libraries without namespaces

查看:338
本文介绍了ZF2:自动加载没有命名空间的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我只使用第三方库与Zend Framework 2一起使用命名空间。现在我需要使用 a库,不使用命名空间,我似乎无法使它的工作。我通过Composer安装它,它安装到供应商目录中。我想使用它如下:



$ obj = new \SEOstats();



结果是一个致命错误,表示无法找到类。我已经尝试手动配置 StandardAutoloader ,但到目前为止没有任何运气。我认为自动加载将自动完成,当安装通过Composer,但我想,不是没有名称空间的情况下?在Composer生成的自动加载文件中我没有看到对库的任何引用。

解决方案



/ div>

您可以使用文件类映射键。



.json:

  {
require:{
vendor-example / non-psr0- libraries:dev-master,
},
autoload:{
files:[vendor / vendor-example / non-psr0-libraries / Library01.php ]
}
}



使用文件键, p>

  $ lib = new \Library01(); 

当需要加载包含类的多个文件时,使用classmap键。 composer.json将是:

  {
require:{
vendor-example / non-psr0-libraries:dev-master,
},
autoload:{
classmap ]
}
}

Composer将扫描 .php



有关详细信息,您可以选择 a href =http://getcomposer.org/doc/04-schema.md#files> http://getcomposer.org/doc/04-schema.md#files 和http://getcomposer.org/doc/04-schema.md#classmap



如果你在创建对象时在命名空间下,你必须使用\(根命名空间),否则你将使用当前命名空间下的Library01类(如果你有一个, t有一个你会得到一个错误)。


Previously I have only been using third party libraries that use namespaces together with Zend Framework 2. Now I need to use a library that does not use namespaces, and I cannot seem to make it work. I installed it via Composer, and it is installed fine into the vendor directory. I am trying to use it as follows:

$obj = new \SEOstats();

The result is a fatal error indicating that the class could not be found. I have tried to manually configure the StandardAutoloader, but so far without any luck. I thought that the autoloading would be done for me automatically when installing through Composer, but I guess that is not the case without namespaces? I haven't seen any reference to the library in the autoload files that Composer generated. I guess I have to do it manually - but how?

Thanks in advance.

解决方案

You can use the files and classmap keys.

As an example consider this composer.json:

{
    "require": {
        "vendor-example/non-psr0-libraries": "dev-master",
    },
    "autoload":{
        "files": ["vendor/vendor-example/non-psr0-libraries/Library01.php"]
    }
}

Using the files key you can then use

$lib = new \Library01();

Use the classmap key when you need to load multiple files containing classes. The composer.json would be:

{
    "require": {
        "vendor-example/non-psr0-libraries": "dev-master",
    },
    "autoload":{
        "classmap": ["vendor/vendor-example/non-psr0-libraries/"]
    }
}

Composer will scan for .php and .inc files inside the specified directory configuring the autoloader for each file/class.

For more info you can check http://getcomposer.org/doc/04-schema.md#files and http://getcomposer.org/doc/04-schema.md#classmap

If you are under a namespace while creating the object, you must use the "\" (root namespace), otherwise you will use the Library01 class under the current namespace (if you have one, if you don't have one you'll get an error).

这篇关于ZF2:自动加载没有命名空间的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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