简体   繁体   English

在我的命名空间中使用库

[英]Using libraries in my namespace

I have several libraries inside my project, made in namespace . 我的项目中有几个库是在namespace When I need to use one of them inside my controller (MVC) I'm including a require and using this library which is in the same namespace as well. 当我需要在控制器(MVC)使用它们之一时,我要包含一个require并使用该库,该库也位于同一名称空间中。 I was wondering if I can join the namespaces so I do not need this require inside the controller every time. 我想知道我是否可以加入名称空间,所以我不需要每次都在控制器内部使用这个需求。 My autoload is loaded in another file, then call the controller to continue the rule. 我的autoload已加载到另一个文件中,然后调用controller以继续执行规则。

Actual controller 实际控制人

namespace application\controller\rule;

use application\controller\database;
use application\model\object as object;

require '/xxx/xxx/autoloadLibary1.php';
require '/xxx/xxx/autoloadLibary2.php';
require '/xxx/xxx/autoloadLibary3.php';
require '/xxx/xxx/autoloadLibary4.php';

class MyClass {}

I think I may not have understood you fully but I think you want to use 我想我可能尚未完全了解您,但我想您想使用

require '/xxx/xxx/autoloadLibary1.php'; as use application\\xxx\\xxx; 作为use application\\xxx\\xxx;

so for this you have to use composer or an alternative that I always use instead of composer , basically use this link to download psr-4/psr-0 autoloader safer way than composer, copy file composer-file-loader-master/PackageLoader.php to your project. 因此,为此,您必须使用composer或我经常使用的替代软件来代替composer ,基本上,使用链接可以比composer更加安全地下载psr-4 / psr-0自动加载器,复制文件composer-file-loader-master / PackageLoader。 PHP到您的项目。

use this 用这个

include __DIR__ . '/includes/PackageLoader.php';
$packageLoader = new PackageLoader\PackageLoader();
$packageLoader->load( __DIR__ . '/includes' );//change where your composer.json file located

use this psr-4 autoloading to locate more than two locations in single namespace 使用此psr-4自动加载功能在单个名称空间中定位两个以上的位置

{
    "autoload": {
        "psr-4": {
            "Namespace\\": [
            "./xxx/xxx/",
            "./another-location"
        ]
        }
    }
}

Now, if you use this statement: 现在,如果使用此语句:

use application\controller\database;
use application\model\object as object;

use application\xxx\xxx\autoloadLibary1;
use application\xxx\xxx\autoloadLibary2;
use application\xxx\xxx\autoloadLibary3;
use application\xxx\xxx\autoloadLibary4;

it sould work well! 它工作得很好! Happy Coding! 编码愉快! If it helps be sure to support me be by accepting. 如果有帮助,一定要接受我的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM