Backend Development 4 min read

Using PHP Namespaces and the `use` Keyword to Import Classes, Functions, and Constants

This article explains how to import classes, functions, and constants from different PHP namespaces using the `use` keyword, demonstrates various import syntaxes including aliasing and bulk imports, and provides complete code examples for each scenario.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP Namespaces and the `use` Keyword to Import Classes, Functions, and Constants

PHP namespaces can be imported into another namespace using the use keyword. The article first shows a simple class import by creating two namespaces ( n1 and n2 ) and instantiating a class from n1 inside n2 either with a fully‑qualified name or by importing the class.

<code>&lt;?php
namespace n1;
class OK {};

namespace n2;
// One way
new \n1\OK();

// Second way
use n1\OK;
new OK();
?>
</code>

When the imported element is not a class, the appropriate keyword ( function or const ) must be used. The article adds a function display and constants P and A in n1 , then demonstrates importing each type.

<code>&lt;?php
namespace n1;
class OK {};
function display(){ echo "display"; }
const P = 10;
const A = 11;

namespace n2;
use n1\OK;               // import class
use function n1\display; // import function
use const n1\A, n1\P;    // import constants

display();
echo P;
new OK();
?>
</code>

Multiple elements can be imported at once using a grouped use statement. The article shows how to import a class, a function, and several constants together.

<code>&lt;?php
namespace n1;
class OK {};
function display(){ echo "n1中的display"; }
const P = 10;
const A = 11;

namespace n2;
use n1\{ OK, const P, const A };
?>
</code>

If an imported name collides with an existing name in the current namespace, the as keyword can be used to alias it. The article demonstrates aliasing a function while importing a class and constants.

<code>&lt;?php
namespace n1;
class OK {};
function display(){ echo "n1中的display"; }
const P = 10;
const A = 11;

namespace n2;
use n1\OK; // class
use function n1\display as display2; // function alias
use const n1\P, n1\A; // constants

display2();
echo P . " " . A;
new OK();
?>
</code>

When all elements of a namespace need to be imported, the entire namespace can be imported with a single use statement. The article creates a nested namespace n1\n2 and shows how to instantiate classes from both the original and the imported namespace.

<code>&lt;?php
namespace n1\n2;
class OK { public function __construct(){ echo __NAMESPACE__ . "<br>"; } }

namespace n2;
class OK { public function __construct(){ echo __NAMESPACE__ . "<br>"; } }

// Import the whole namespace
use n1\n2;

new OK();            // refers to n2\OK
new n2\OK();         // uses the imported namespace's last level
?>
</code>

The article concludes with a link to the original WeChat article for further reading.

ImportuseNamespaces
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.