Home  >  Article  >  Backend Development  >  Usage of namespace in thinkphp

Usage of namespace in thinkphp

不言
不言Original
2018-06-09 09:32:181570browse

This article mainly introduces the usage of thinkphp namespace, and analyzes the functions and specific usage techniques of thinkPHP namespace in detail in the form of examples. Friends in need can refer to the following

The examples in this article describe the usage of thinkphp namespace. . Share it with everyone for your reference, the details are as follows:

The new version (3.2) adopts the namespace method to define and load class library files, solves the conflict problem between multiple modules, and achieves a more efficient Automatic loading mechanism.

You need to define the namespace where the class library is located. The path of the namespace is consistent with the directory of the class library file, so that the class can be automatically loaded. For example, the Org\Util\File class is defined as

namespace Org\Util;
class File {
}

The path where it is located is ThinkPHP/Library/Org/Util/File.class.php. We instantiate this class as follows:

$class = new \Org\Util\File();

The system will automatically load the above file, so there is no need to add it to the instance. The class library file is imported before the class defined in the namespace.

The root namespace is a very key concept. Take the Org\Util\File class above as an example. Org is a root namespace, and its corresponding initial namespace directory is the system's class library directory ThinkPHP/ Liberary, the subdirectories below this directory will be automatically recognized as the root namespace, and these namespaces can be used without registration.

We add a My root namespace directory under the Library directory, and then define a Test class as follows:

namespace My;
class Test
{
   public function sayHello()
  {
    echo 'hello';
  }
}

Save the test class in ThinkPHP/Liberary/My/Test.class.php , we can directly instantiate and call the class library namespace in the

$Test = new \My\Test();
$Test->sayHello();

module is named after the module name, for example:

namespace Home\Model;
class UserModel extends \Think\Model
{
}

Its class file is located in Application/Home/Model/UserModel. class.php

namespace Admin\Event;
class UserEvent {
}

The class file is located in Application/Admin/Event/UserEvent.class.php

3.2.1 or above allows setting not to use the namespace for the application class library, in the configuration file The settings are as follows:

'APP_USE_NAMESPACE'    =>    false,

In this way, the application class library no longer needs to use the namespace definition, but the namespace still needs to be used when inheriting and calling the core class library. For example, the following application class library will not Then write namespace Admin\Model;

class UserModel extends \Think\Model {
}

Special note: If you need to instantiate PHP's built-in class library or a third-party class that is not defined using a namespace in version 3.2, you need to use the following method:

$class =  new \stdClass();
$sxml =  new \SimpleXmlElement($xmlstr);

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

thinkPHP5.0 framework overall architecture overview [application, module, MVC, driver, behavior, namespace, etc.]

thinkPHP's Html template tag usage method

The above is the detailed content of Usage of namespace in thinkphp. 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