Simple Usage Analysis of PHP Namespace namespace and use

  • 2021-10-25 06:16:38
  • OfStack

This article illustrates the simple use of the PHP namespaces namespace and use. Share it for your reference, as follows:

Recently, I have been studying php framework for a period of time. When can I develop my own framework? Of course, this is to improve my programming level, and at the same time, I can mix the scattered things I usually learn in a skilled application. But developing a framework doesn't know how to start at all. What to develop first, although there are many frameworks of php, but how to apply them, there are no documents and videos to learn, which makes me particularly depressed. I believe many friends who want to develop their own frameworks to play have similar feelings, and they are depressed, but they still do it by themselves. It is difficult to do it as difficult as imagining it, and they are a little confused from 1.

That is the use of namespaces and the introduction of use. After reading a lot of online statements and official documents, the meaning is probably clear and easy to understand. The namespace is easy to say. Give this space a name, but use can't be operated concretely. After watching an yii learning video, I suddenly understood.

Let's say build three files.

The first file, A. php, contains two classes with a namespace of a\ b\ c;


<?php
namespace a\b\c;
class Apply{
  function get_info(){
    echo 'this is A Apply';
  }
}
class C{
  function info(){
    echo 'this is info';
  }
}

File 2 B. php Namespace a\ b\ d;


<?php
namespace a\b\d;
class Apply{
  function get_info(){
    echo 'this is B Apply';
  }
}

The third file, index. php, is used to use the classes of the above two files.

For example, if we want to instantiate the class in A. php now, how should we do it?

The first thing to do is to include this file


require_once('A.php');

And then use a\b\c; ? Or a\b\c\A ? That's what I thought at first. In fact, this is wrong, use should be like this, namespace\ in this space you want to instantiate the class name of the class. For example, if we want to instantiate the Apply class in A. php, then use a\b\c\Apply; This is equivalent to introducing this class, and then new Apply(); Call the method inside, which is the same as usual. If you want to instantiate the class C, use a\ b\ c\ C;

Note: use Not equal to require_once Or include , use The premise is that the file has been included in the current file.

By the way, in MVC schema, the class name and file name are the same, so use will make people who don't know it think that use is followed by a file name, which I thought before. In fact, use is still a class name.

Some people may ask, that I have the same class name under different namespaces, what should I do if I use it in the same file? For example, index. php above includes A. php and B. php, and then new Apply(); At this time, an error will be reported. The solution is to give an alias, for example use a\b\d\Apply as b; At this time we a\b\c\A0 It should not be written as new Apply(); But new b(); So there will be no conflict.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: