Home >Backend Development >PHP Problem >What is the usage of php use

What is the usage of php use

coldplay.xixi
coldplay.xixiOriginal
2020-10-08 14:23:475861browse

Usage of php use: The namespace is introduced using use, and the code is [namespace a\b\c;class Apply{function get_info(){echo 'this is A Apply';}].

What is the usage of php use

How to use php use:

I have been studying the php framework recently, and I have been thinking about when it can be developed. To develop my own framework, of course, this is to improve my programming level, and at the same time, I can combine the scattered things I usually learn and apply them skillfully. But when developing a framework, I don’t know how to start and what to develop first. Although there are many PHP frameworks, they are all about how to apply them. There are no documents or videos to learn from. This is something that makes me particularly depressed. I believe many people want to know how to apply it. Friends who play with development frameworks all have similar feelings. I feel depressed, so I decided to do it myself. It was as difficult as I imagined, and I was a little confused from the beginning.

That is the use of namespaces and the introduction of use. After reading a lot of opinions on the Internet and reading official documents, the meaning is roughly clear and easy to understand. The namespace is easy to say, just give this space a name, but the specific operation of use is not working, and I can't figure it out. Later, I suddenly understood it after watching a Yii learning video.

For example, let’s create three files.

The first file A.php has two classes in it. The namespace is a\b\c;

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

The second file B.php is named Space a\b\d;

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

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

For example, we now want to instantiate a class in A.php. How to achieve this?

First include this filerequire_once('A.php');

Then use a\b\c;? Or a\b\c\A? I thought so too at first. In fact, this is wrong, use should be like this, namespace\class name of the class you want to instantiate under this space. 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(); to call the method inside, which is the same as usual . If you want to instantiate class C, use a\b\c\C;

Note: use is not equal to require_once or include. The premise of use is that the file has been included in the current file.

By the way, in the MVC mode, the class name and the file name are the same, so when using, people who don’t know will think that use is followed by the file name, which is what I thought before. In fact, the use is still the class name.

Some people may ask, what should I do if I have the same class name in different namespaces and use it in the same file? For example, our index.php above includes A.php and B.php, and then new Apply(); will report an error at this time. The solution is to use an alias, for example, use a\b\d\Apply as b; At this time, when we use new, we should not write new Apply(); but new b(); so that there will be no conflict.

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of What is the usage of php use. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn