Home  >  Article  >  Backend Development  >  How to use use in php (code example)

How to use use in php (code example)

烟雨青岚
烟雨青岚forward
2020-06-15 17:55:253119browse

How to use use in php (code example)

How to use use in php (code example)

I have been studying the php framework recently. I wonder when I can 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, create three files.

The first file A.php has two classes in it, and 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 namespace 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 above two files.

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

First include this file require_once('A.php');

Then use a\b\c;? Or a\b\c\A? I thought so 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.

Thank you everyone for reading, I hope you will benefit a lot.

Recommended tutorial: "php tutorial"

The above is the detailed content of How to use use in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete