Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Namespace Global

Tags:

namespaces

php

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()
like image 456
JREAM Avatar asked Oct 08 '22 01:10

JREAM


2 Answers

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

like image 164
Nick Avatar answered Oct 12 '22 21:10

Nick


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

use p\handle;

which aliases p\handle to just handle.

like image 23
Jon Avatar answered Oct 12 '22 20:10

Jon