PHP Namespace Global

Is there a way to get the PHP namespace to allow callers in the namespace as if they were global?

Example:

handle()

Instead

prggmr\handle()

Here is my sample code:

<?php
require_once 'prggmr/src/prggmr.php';

prggmr\handle(function() {
  echo 'This is a test scenario';
}, 'test');

prggmr\signal('test');

Smoothing does not work for functions:

<?php
require 'prggmr/src/prggmr.php';
use prggmr\handle;

handle(function(){
    echo "Test";
}, "test");

Results:

Fatal error: Call to undefined function handle()
+5
source share
3 answers

From my understanding, this excerpt from the documentation says that this is impossible ...

Neither functions nor constants can be imported using the use statement.

Source:

http://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nofuncconstantuse

+2
source

Not for the whole namespace, no. But for single names you can do

use p\handle;

which aliases are p\handle simple handle.

+2
source

, PHP, , :

PHP 5.6 use. PHP 5.6 .

So your second code sample should work fine these days.

It's also worth mentioning that you can define a block area for PHP namespaces, although this has discouraged . So this will work:

<?php
require_once 'prggmr/src/prggmr.php';

namespace prggmr {
    handle(function() {
      echo 'This is a test scenario';
    }, 'test');

    signal('test');
}
0
source

All Articles