Calling Namespace


 

Calling Namespace

In this tutorial we will study about namespace declaration and calling from other file in PHP, how to call a namespace in PHP, how to access from outside the file etc. Examples will make this more clear.

In this tutorial we will study about namespace declaration and calling from other file in PHP, how to call a namespace in PHP, how to access from outside the file etc. Examples will make this more clear.

Calling Namespaced Code From Other File:

In the previous tutorial we have seen that how to declare a namespace and how to access the class, function from inside the code, now in this current tutorial we will study how to access those class and function from other file.

Example:

Lib1.php

<?php

namespace mySpace;

function myFunction()

{

echo __FUNCTION__;

}

class One

{

function myMethod()

{

echo __METHOD__;

}

}

?>

Lib2.php

<?php

header('content-type:text/plain');

include('Lib1.php');

echo mySpace\myFunction()."\n";

echo mySpace\One::myMethod();

?>

Output:

mySpace\myFunction
mySpace\One::myMethod

Ads