php 命名空間(要求php5.3以上)

來源:互聯網
上載者:User

標籤:使用   io   檔案   ar   art   cti   代碼   on   

要求php5.3以上

<?php
namespace test;
// 命名空間與目錄類似功能,也可定義子命名空間,用分層的方式定義:
/*
namespace mydir\ok\project;

在聲明命名空間之前唯一合法的代碼是用於定義源檔案編碼方式的 declare 語句。另外,所有非 PHP 程式碼封裝括空白符都不能出現在命名空間的聲明之前:
*/
const OK = 100;
class tclass {

}
function tfunction()
{

}

// 類,函數,常量受命名空間影響,必須在所有代碼之前聲明命名空間
// 可以在同一個檔案中定義多個命名空間(不建議),有兩種方式,第一種[不推薦]:
<?php
namespace myproject;
const CONN = 1;
class myclass {}
function myfunction(){}

namespace AntherProject;
const CONN_2 = 100;
class Myclass2{}
function myfunction2(){}
?>
<?php
// 第二種定義多個命名空間,大括弧方法
namespace MyProject{
  const CONN = 1;
  class myclass{}
  function myfunction(){}
}

namespace AnotherProject{
  const CONN = 2;
  class myclass2{}
  class myfunction(){}
}
?>

<?php
// 將全域的非命名空間中的代碼與命名空間中的程式碼群組合在一起,只能使用大括弧形式的文法。全域代碼必須用一個不帶名稱的 namespace 語句加上大括弧括起來,例如:
namespace MyProject{
  const CON = 1;
  class MyClas{}
  function myfunction(){}
}
// 以下為全域代碼
namespace{
  session_start();
  $a = MyProject\myfunction();
  echo MyProject\Connect::start();
}
?>
//定義多個命名空間和不包含在命名空間的代碼
<?php
declare(encoding="UTF-8");
namespace MyProject{
   const CONN = 1;
   class myclass{}
   function myfunction(){}
}
//以下全域代碼
namespace {
  session_start();
  $a = MyProject\myfunction();
  echo MyProject\Connect::start();
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.