search

Home  >  Q&A  >  body text

php namespace

Question: Why do I have to manually import the corresponding files in the same namespace when running the file after the namespace has been declared? For example: I have three separate class files in the same folder that do not conflict with each other. The three class files are all declared in the same namespace, but when I run the file corresponding to class 1, if class 2 is used, Then a fatal error is reported: Class 2 not found

Thank you

nefu_wangxunnefu_wangxun978 days ago1093

reply all(5)I'll reply

  • autoload

    autoload2022-04-24 09:31:48

    Introducing automatic loading

    reply
    1
  • nefu_wangxun

    Thanks, this is indeed a solution. In fact, what I want to know more is: Since the classes scattered in different files have been defined in the same namespace, why do we need to introduce automatic loading again? Shouldn't classes in the same namespace be directly visible to each other? If automatic loading is also introduced, what is the significance of the namespace?

    nefu_wangxun · 2022-04-24 10:05:18
    autoload

    For example, there are n supermarket chains in the country, but it does not mean that the data of the n supermarket chains are common. You need to set up a head office, and the head office will load all the data into one province. If a supermarket wants to access provincial data, it only needs to go to the corresponding provincial database of the head office to have a comprehensive view.

    autoload · 2022-04-25 09:22:12
    nefu_wangxun

    Thank you for your reply again. It's exactly what you said. The meaning of the namespace may be to emphasize the logical structure of files, making it easier to distinguish and better manage files. Just like the head office database can better manage the data of multiple branches, but if branch 1 wants to know the data information of branch 2, it still has to go to the head office to call it. This is like importing different files in the same namespace when they are used with each other. But at this time you can clearly know where to go to find the files you need in the potentially huge file system, just like you clearly know where to go to the database of your head office to find the data of the branch you need. The information is the same, because the file you need is in your namespace, and you can clearly locate the target file location. Therefore, the meaning of a namespace does not necessarily mean that files in the same space can be directly shared and visible. After thinking about it, you will find that if files in the same namespace are directly visible, it means that when running a file, you may have to Automatically load all files in this space, which consumes resources and most of the loaded files are not used. The significance of the namespace is that when you need a file, thanks to this file structure, it can help you quickly import the file you want, and then you can import individual files according to your own needs.

    nefu_wangxun · 2022-04-25 21:28:04
    nefu_wangxun

    As you said -> as you said

    nefu_wangxun · 2022-04-25 21:34:09
  • Cancelreply