Home >Backend Development >PHP Tutorial >Several points to note when defining command space in PHP
This article mainly introduces several points to pay attention to when defining command space in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
1. Declaring the command space must be the first statement of the program script. In addition, all non-PHP code, including whitespace, cannot appear before the namespace declaration.
The following is an example of an error:
<html> <?php namespace MyProject; // 致命错误 - 命名空间必须是程序脚本的第一条语句 ?>
This is also wrong
<?php // Lots // of // interesting // comments and white space namespace Foo; class Bar { } ?>
2. PHP keywords cannot be used.
The following is an error example:
<?php namespace Project/Classes/Function; // Causes parse errors namespace Project/Abstract/Factory; // Causes parse errors ?>
3. Constant definition in the namespace .
The following MESSAGE is in the global namespace:
<?php namespace test; define('MESSAGE', 'Hello world!'); ?>
Define 2 constants in the test namespace:
<?php namespace test; define('test\HELLO', 'Hello world!'); define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!'); ?>
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpHow to use magic functions and magic constants
How to use PHP magic methods __call and __callStatic
Detailed explanation of several methods of PHP to achieve page staticization
The above is the detailed content of Several points to note when defining command space in PHP. For more information, please follow other related articles on the PHP Chinese website!