namespace

创建于 2024-12-03 / 49
字体: [默认] [大] [更大]

❮ PHP 关键字

实例

在 Html 命名空间中创建一个 Table 类:

<?php
namespace Html;
class Table {
  public $title = "";
  public $numRows = 0;
  public function message() {
    echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
  }
}
$table = new Table();
$table->title = "My table";
$table->numRows = 5;
?>

<!DOCTYPE html>
<html>
<body>

<?php
$table->message();
?>

</body>
</html> 亲自试一试 »

定义和用法

namespace 关键字用于声明 PHP 文件在哪个命名空间中运行。 命名空间可防止同名类之间发生冲突,并可用于通过将相关类分组在一起来组织代码。


相关页面

在我们的 PHP 命名空间教程中了解有关命名空间的更多信息。


❮ PHP 关键字
0 人点赞过