Analysis of Design Idea Usage and Shortcomings of php Namespace

  • 2021-12-13 07:38:15
  • OfStack

This paper describes the design idea, usage and shortcomings of php namespace with examples. Share it for your reference, as follows:

Compared with languages such as C #, you can define variables and assign values at will in php functions without worrying about overriding global variables or class variables; You can also define class variables at will without worrying about conflicting with the function name, because the variables are preceded by a $.

php namespace and global variables, local variables design ideas 1 sample, are as far as possible to reduce naming conflicts. php namespaces can be similar to file systems, with relative namespaces and absolute namespaces.

Define Namespace

Must be the first statement, there can be no statement before it, and comments can.
Must be an absolute namespace.


namespace bookrpg\config;
// Or 
namespace bookrpg\config
{
}
namespace \bookrpg\config; // Error, no leading \

Import Namespace

Must be an absolute namespace.


use bookrpg\config\ClassName;
use \bookrpg\config\ClassName; // You can have a preamble \ , but it is not recommended to add 
use bookrpg\config\ClassName as NewClassName;
use Exception;
use \Exception; // You can have a preamble \ , but it is not recommended to add 

Use namespaces

Use Absolute Namespace


$cls = new \bookrpg\config\ClassName();
class MyException extends \Exception
{
}

Use relative namespaces


namespace bookrpg
{
  // Equivalent to new \bookrpg\config\ClassName();
  $cls = new config\ClassName();
  // Errors, php Will think it is \bookrpg\Exception Instead of the global class Exception
  $ex = new Exception();
}

A weird place

C + +, Java and C # all have similar namespace concepts, while php's namespace does not follow the crowd and has its own set of independent ideas, which is strange and confusing to preconceived people.

There is a backslash in the namespace?

The dot (.) is already used as a string connector, and to distinguish between absolute and relative namespaces, new. a. b. c () is too unattractive, so choose a backslash.

1 denier use namespace, use global class also want to add "\" or use1 under, tired not tired?

No way, in order to strictly analogize the concept of file system, and to define class names casually without worrying about class name conflicts.

Since it's a strict analogy file system, why don't you add\ before defining and importing namespaces?

It can only be said that the designers of php are not perfectionists.

But the namespace of php does lack logical perfection:

The use of the leading\ is not unified 1, the same is an absolute namespace, the definition is not allowed to use, but the import is allowed to use. Even global classes must use the leading\ in non-global namespaces, and global functions and variables are not subject to this restriction.

Suggestions for use

When defining and importing namespaces, System 1 does not preface\ All classes are imported using use, including global classes, only namespace classes, and conflicts are under as1 Direct use of relative namespaces is prohibited

Understand the design idea of php namespace and avoid its imperfect design, so it seems that php namespace is also easy to accept.

More readers interested in PHP can check out 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: