Namespaces

Defining namespaces


arrayutil.php

<?php
namespace ArrayUtil;

function min($array)
{
  $minValue = $array[0];

  for ($flag = 1; $flag < sizeof($array); $flag++) {
    if ($minValue > $array[$flag])
      $minValue = $array[$flag];
  }

  return "Minimum: $minValue";
}

// Unqualified name
var_dump(min([1, 4, 2, 6, 10, 3])); //=> string(10) "Minimum: 1"

// Qualified name
var_dump(ArrayUtil\min([1, 4, 2, 6, 10, 3])); //=> 1
//=> Fatal error: Uncaught Error: Call to undefined function ArrayUtil\ArrayUtil\min()

// Fully qualified name
// Global space
var_dump(\min([1, 4, 2, 6, 10, 3])); //=> int(1)
// ArrayUtil space
var_dump(\ArrayUtil\min([1, 4, 2, 6, 10, 3])); //=> string(10) "Minimum: 1"

Tips:

External namespace


tree .
.
├── arrayutil.php
└── main.php

0 directories, 2 files

arrayutil.php:

<?php
namespace ArrayUtil;

function min($array)
{
  $minValue = $array[0];

  for ($flag = 1; $flag < sizeof($array); $flag++) {
    if ($minValue > $array[$flag])
      $minValue = $array[$flag];
  }

  return "Minimum: $minValue";
}

main.php:

<?php
require 'arrayutil.php';

// Unqualified name
var_dump(min([1, 4, 2, 6, 10, 3])); //=> int(1)

// Qualified name
var_dump(ArrayUtil\min([1, 4, 2, 6, 10, 3])); //=> string(10) "Minimum: 1"

// Fully qualified name
var_dump(\min([1, 4, 2, 6, 10, 3])); //=> int(1)
var_dump(\ArrayUtil\min([1, 4, 2, 6, 10, 3])); //=> string(10) "Minimum: 1"

Sub-namespaces


$ tree arrayutil
arrayutil
├── arithmetic
│   └── arithmetic.php
├── main.php
└── stats
    └── stats.php

2 directories, 3 files

arrayutil/arithmetic/arithmetic.php:

<?php
namespace ArrayUtil\Arithmetic;

function array_sum($array)
{
  $sum = 0;
  foreach ($array as $value) {
    $sum += $value;
  }
  return "Sum: $sum";
}

arrayutil/stats/stats.php:

<?php
namespace ArrayUtil\Stats;

function min($array)
{
  $minValue = $array[0];

  for ($flag = 1; $flag < sizeof($array); $flag++) {
    if ($minValue > $array[$flag])
      $minValue = $array[$flag];
  }

  return "Minimum: $minValue";
}


arrayutil/main.php:

<?php
namespace ArrayUtil;

require 'arithmetic/arithmetic.php';
require 'stats/stats.php';

// Unqualified name
var_dump(array_sum([1, 2, 3, 4])); //=> int(10)

// Qualified name
var_dump(Arithmetic\array_sum([1, 2, 3, 4])); //=> string(7) "Sum: 10"

// Fully qualified name
var_dump(\array_sum([1, 2, 3, 4])); //=> int(10)
var_dump(\ArrayUtil\Arithmetic\array_sum([1, 2, 3, 4])); //=> string(7) "Sum: 10"

Multiple Namespaces


arrayutil/main.php:

<?php
namespace ArrayUtil;

require 'arithmetic/arithmetic.php';
require 'stats/stats.php';

// Unqualified name
var_dump(array_sum([1, 2, 3, 4])); //=> int(10)

// Qualified name
var_dump(Arithmetic\array_sum([1, 2, 3, 4])); //=> string(7) "Sum: 10"

// Fully qualified name
var_dump(\array_sum([1, 2, 3, 4])); //=> int(10)
var_dump(\ArrayUtil\Arithmetic\array_sum([1, 2, 3, 4])); //=> string(7) "Sum: 10"

Aliasing/Importing


ArrayUtil.php:

<?php

namespace ArrayUtil\Arithmetic;

function array_sum($array): float
{
  $sum = 0;
  foreach ($array as $value) {
    $sum += $value;
  }
  return $sum;
}

namespace ArrayUtil\Stats;

function min($array): float
{
  $minValue = $array[0];

  for ($flag = 1; $flag < sizeof($array); $flag++) {
    if ($minValue > $array[$flag])
      $minValue = $array[$flag];
  }

  return $minValue;
}

namespace ArrayUtil;

use function \ArrayUtil\Stats\min;
use function \ArrayUtil\Arithmetic\array_sum as a_sum;

$a = [1, 2, 3, 4];

var_dump(\min($a));                 //=> int(1)
var_dump(\ArrayUtil\Stats\min($a)); //=> float(1)
var_dump(Stats\min($a));            //=> float(1)
var_dump(min($a));                  //=> float(1)

var_dump(\array_sum($a));                      //=> int(10)
var_dump(\ArrayUtil\Arithmetic\array_sum($a)); //=> float(10)
var_dump(Arithmetic\array_sum($a));            //=> float(10)
var_dump(array_sum($a));                       //=> int(10)
var_dump(a_sum($a));                           //=> float(10)

References