JREAM JREAM - 9 months ago 75
PHP Question

PHP Namespace Global

Is there a way to get a PHP namespace to allow for calling functions within a namespace as if they were global?

Example:

handle()


Rather than:

prggmr\handle()


Here is my example code:

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

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

prggmr\signal('test');


Aliasing 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()

Answer Source

From my understanding this excerpt from the documentation says it's not possible ...

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

Source:

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