LoginSignup
7
7

More than 5 years have passed since last update.

PHP namespace ネームスペースについてのメモ

Last updated at Posted at 2015-12-28

naemspaceについてのメモ
参照:http://kudox.jp/php/namespace

恥ずかしながら

名前空間を定義するには、namespaceキーワードを使用して名前空間を宣言します。名前空間の影響を受けるのは、クラス、インターフェイス、関数、constで定義された定数です。変数やdefine()関数で定義された定数は影響を受けません。

を知らなかった。

sample.php
<?php
namespace Test;
class Directory
{
  public function __construct()
  {
    echo 'Test1\Directory', PHP_EOL;
  }
}



namespace Test2;
class Directory
{
  public function __construct()
  {
    echo 'Test2\Directory', PHP_EOL;
  }
}




namespace Test;
$direcotry = new DIrectory();


namespace Test2;
$direcotry = new DIrectory();

?>

Paiza.io サンプルコード

同じclassをnewしてるが、namespaceによって参照されるclassが異なります。


追記です。
namespace Test1,Test2を
namespave Test3で利用したい場合

tuika.php

<?php
namespace Test1;
class Directory1
{
  public function __construct()
  {
    echo 'Test1\Directory', PHP_EOL;
  }
}



namespace Test2;
class Directory2
{
  public function __construct()
  {
    echo 'Test2\Directory', PHP_EOL;
  }
}





//namespace Test1,Test2をTest3で利用したい場合
namespace Test3;
use Test1;
use Test2;
$direcotry1 = new Test1\DIrectory1();
$direcotry2 = new Test2\DIrectory2();



7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7