여러분이 사용하고 계신 브라우저는 HTML5를 지원하지 않기 때문에 몇몇 요소가 제대로 보이도록 JScript를 사용하고 있습니다. 하지만 여러분의 브라우저 설정에서 스크립트 기능이 꺼져있으므로, 현재 페이지를 제대로 확인하시려면 스크립트 기능을 켜주셔야 합니다. PHP - 기초 - OOP 네임스페이스
PHP – 기초 – OOP 네임스페이스
2년전 작성
8개월전 수정

네임스페이스

네임스페이스는 두 가지 다른 문제를 해결하는 한정자다.

  1. 작업을 수행하기 위해 함께 작동하는 클래스를 그룹화하여 더 나은 조직을 허용한다.
  2. 둘 이상의 클래스에 동일한 이름을 사용할 수 있다.

 
예를 들어 Table, Row 및 Cell과 같은 HTML 테이블을 설명하는 클래스 집합이 있을 수 있으며 Table, Chair 및 Bed와 같은 가구를 설명하는 또 다른 클래스 집합이 있을 수 있다.
네임스페이스를 사용하여 클래스를 두 개의 다른 그룹으로 구성하는 동시에 두 개의 클래스 Table과 Table이 섞이는 것을 방지할 수 있다.

네임스페이스 선언

namespace네임스페이스는 다음 키워드 를 사용하여 파일 시작 부분에 선언된다.

namespace namespace이름;

이런식으로 선언한다.

예시

예시를 통해 공부해보자.

PHP
<?php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;
public function message() {
echo "

Table '{$this->title}' has {$this->numRows} rows.

"; } } $table = new Table(); $table->title = "My table"; $table->numRows = 5; ?> <!DOCTYPE html> <html> <body> <?php $table->message(); ?> </body> </html>
기본 예시
Table ‘My table’ has 5 rows.

중첩된 네임스페이스

Code라는 네임스페이스 안에 Html이라는 네임스페이스를 선언해보자.

namespace Code\Html;

이런식으로 입력한다.

네임스페이스 사용

선언 뒤에 오는 모든 코드 namespace는 네임스페이스 내에서 작동하므로 네임스페이스에 속한 클래스는 한정자 없이 인스턴스화할 수 있다.
네임스페이스 외부에서 클래스에 액세스하려면 클래스에 네임스페이스가 연결되어 있어야한다.

index.php
<?php
include "html.php";

$table = new Html\Table();
$table->title = "My table";
$table->numRows = 5;

$row = new Html\Row();
$row->numCells = 3;
?>

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
html.php
<?php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;

public function message() {
echo "

Table '{$this->title}' has {$this->numRows} rows.

"; } } class Row { public $numCells = 0; public function message() { echo "

The row has {$this->numCells} cells.

"; } } ?>
기본 예시
Table ‘My table’ has 5 rows.

The row has 3 cells.

 
동일한 네임스페이스의 많은 클래스가 동시에 사용되는 경우 namespace키워드를 사용하는 것이 더 쉽다.

index.php
<?php
namespace Html;
include "html.php";

$table = new Table();
$table->title = "My table";
$table->numRows = 5;

$row = new Row();
$row->numCells = 3;
?>

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
html.php
<?php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;

public function message() {
echo "

Table '{$this->title}' has {$this->numRows} rows.

"; } } class Row { public $numCells = 0; public function message() { echo "

The row has {$this->numCells} cells.

"; } } ?>
기본 예시
Table ‘My table’ has 5 rows.

The row has 3 cells.

네임스페이스 별칭

작성하기 쉽도록 네임스페이스나 클래스에 별칭을 지정하는 것이 유용할 수 있다.
use 키워드로 수행된다.

네임스페이스에 별칭 지정
index.php
<?php
include "html.php";
use Html as H;
$table = new H\Table();
$table->title = "My table";
$table->numRows = 5;
?>

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
html.php
<?php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;

public function message() {
echo "

Table '{$this->title}' has {$this->numRows} rows.

"; } } class Row { public $numCells = 0; public function message() { echo "

The row has {$this->numCells} cells.

"; } } ?>
기본 예시
Table ‘My table’ has 5 rows.
클래스에 별칭 지정
index.php
<?php
include "html.php";
use Html\Table as T;
$table = new T();
$table->title = "My table";
$table->numRows = 5;
?>

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
html.php
<?php
namespace Html;
class Table {
  public $title = "";
  public $numRows = 0;

  public function message() {
    echo "

Table '{$this->title}' has {$this->numRows} rows.

"; } } class Row { public $numCells = 0; public function message() { echo "

The row has {$this->numCells} cells.

"; } } ?>
기본 예시
Table ‘My table’ has 5 rows.
참고

Mingg`s Diary
밍구
공부 목적 블로그