Contents
  1. 1. Namespace
    1. 1.1. Delare
    2. 1.2. Import and Alias
      1. 1.2.1. Normal Import
      2. 1.2.2. function and constant
      3. 1.2.3. Helpful tips
        1. 1.2.3.1. Multiple imports
        2. 1.2.3.2. Multiple namespace in one file
        3. 1.2.3.3. Global namespace

Namespace

Delare

1
2
3
<?php
namespace Animal;
namespace Animal\Dog;

Import and Alias

Normal Import

1
2
3
<?php
use Animal\Dog;
use Animal\Dog as DD;

function and constant

1
2
3
4
5
6
7
// PHP 5.6+ only: Import a function
use func Namespace\functionName;
functionName();

// PHP 5.6+ only: Import a constant
use constant Namespace\CONST_NAME;
echo CONST_NAME;

Helpful tips

Multiple imports

1
2
3
4
5
6
7
8
9
10
//Don't do this
<?php
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\HttpFoundation\Cookie;

//It should be
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;

Multiple namespace in one file

1
2
3
4
5
6
7
<?php
namespace Foo {
// Declare classes, interfaces, functions, and constants here
}
namespace Bar {
// Declare classes, interfaces, functions, and constants here
}

Global namespace

1
2
3
4
5
6
7
8
9
<?php
namespace My\App;
class Foo
{
public function doSomething()
{
throw new \Exception();
}
}
Contents
  1. 1. Namespace
    1. 1.1. Delare
    2. 1.2. Import and Alias
      1. 1.2.1. Normal Import
      2. 1.2.2. function and constant
      3. 1.2.3. Helpful tips
        1. 1.2.3.1. Multiple imports
        2. 1.2.3.2. Multiple namespace in one file
        3. 1.2.3.3. Global namespace