Php: How do I reference a third-party namespace?

Source: Internet
Author: User
I have obtained several PHP files from a third party, all of which start with namespace. How can I use it? In addition, I want to use github. comjeremeamiasuper_closure I see useSuperClosureSerializer; however, Fatalerror: Class & #039; SuperC... I have obtained several PHP files from a third party, starting with namespace,
How can I use it?

In addition, what I want to use is
Https://github.com/jeremeamia/super_closure

Instructions
Use SuperClosure \ Serializer;
But the prompt
Fatal error: Class 'superclosure \ serializer' not found

If I add the same namespace to all my PHP files, I will report an error and cannot find the system's own PHP file.

Reply content:

I have obtained several PHP files from a third party, starting with namespace,
How can I use it?

In addition, what I want to use is
Https://github.com/jeremeamia/super_closure

Instructions
Use SuperClosure \ Serializer;
But the prompt
Fatal error: Class 'superclosure \ serializer' not found

If I add the same namespace to all my PHP files, I will report an error and cannot find the system's own PHP file.

Currently, third-party packages are widely used in php.composerYou can checkcomposerRelated usage and concepts

composer require jeremeamia/superclosure

If you want to use a file separately, you can directlyinclude. However, a package usually has many class files, and you don't plan to use them all.include--#

psr-4Is a load standardnamespaceCorresponds to a file path to achieve delayed loading on demand; UsecomposerInstallation, only onecomposerYou can implementpsr-4.

Of coursecomposerThere are many other functions ....

If your program is not usednamespaceAnd you need to usenamespaceFirst, make sure that these program files areincludeIn your program, you can directly use the full name of the corresponding class or useuseTo access these classes.

If you add the PHP file require_once () of namespace SuperClosure \ Serializer;, no error will be reported.

Namespace is used to register the path of a file. If you need to use a third-party class package, if your code supports the namespace autoload method, you need to add namespace to the relevant autoload. Different frameworks have different autoload methods. If not, you need to register your own autoload method. If you do not want to use the autoload method, you need to include each file you are using.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.