php namespace use 研究

來源:互聯網
上載者:User

標籤:test   php   getc   logs   code   get   log   命名衝突   class   

1.file1.php:

<?php    namespace foos;    class demo{        function testfn(){            echo "sdlkfjskdjf";        }    }?>

2.main.php:

<?php    include ‘./file1.php‘;    $getcls=new foos\demo;    $getcls->testfn(); //這裡可以正常列印出來正常執行,說明namespace是一個頂層的層次命名?>

或者你也可以這樣寫:

<?php    include ‘./file1.php‘;    use foos/demo; //使用這個namespace    $getcls=new demo;    $getcls->testfn();?>

使用namespace use的調用需要引入其他檔案才可以調用其他檔案的東西,才可以use其他檔案的東西,否則無效。

以前沒有namespace的時候直接引入檔案然後直接調用,現在php有了namespace的主要目的是更好的區別模組,避免重新命名衝突的現象,而不是為了實現模組的調用,調用還是include或require這些,namespace 和use主要還是為了建立命名上的層次關係,避免命名空間上的衝突。

use 某個namesp 和 直接 \namespace\cls 是一樣的功能,所以我們可以看出namespace主要是為了區別層次關係,更好的定義檔案的空間命名避免不必要的命名衝突。

php namespace use 研究

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.