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

Several points to note when defining command space in PHP

墨辰丷
Release: 2023-03-28 15:26:02
Original
1218 people have browsed it

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; // 致命错误 - 命名空间必须是程序脚本的第一条语句
?>
Copy after login

This is also wrong

<?php 
// Lots 
// of 
// interesting 
// comments and white space 

namespace Foo; 
class Bar { 
} 
?>
Copy after login

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 
?>
Copy after login

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;);
?>
Copy after login

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;);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template