ZF2: autoloading libraries without namespaces

后端 未结 1 1478
感动是毒
感动是毒 2021-01-13 17:48

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 c

相关标签:
1条回答
  • 2021-01-13 18:28

    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).

    0 讨论(0)
提交回复
热议问题