Home  >  Article  >  Backend Development  >  Several points to note when defining command space in PHP

Several points to note when defining command space in PHP

墨辰丷
墨辰丷Original
2018-05-31 09:55:441158browse

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(&#39;MESSAGE&#39;, &#39;Hello world!&#39;);
?>

Define 2 constants in the test namespace:

<?php
namespace test;
define(&#39;test\HELLO&#39;, &#39;Hello world!&#39;);
define(__NAMESPACE__ . &#39;\GOODBYE&#39;, &#39;Goodbye cruel world!&#39;);
?>

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!

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