25

Is it possible to define a global function from within a PHP namespace (within a file that has a namespace declaration)? If so, how?

<?php
namespace my_module;

# bunch of namespaced functions
# ...
# then I want to define a global function my_module()
# that is an alias to my_module\mymodule()
ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 2
    You probably shouldn't do this, as it cries "bad design, bad structure" all over. Your particular reason to do so would be? – Sven Dec 03 '12 at 23:53
  • @Sven See updated question. I'd rather have everything in 1 file than create a separate file just to define 1 function. – ryanve Dec 03 '12 at 23:56
  • @ryanve: I'm with Sven. I recommend providing the big picture view and asking if there's a better way to accomplish that than from this work-around. – RonaldBarzell Dec 04 '12 at 00:00
  • @user1161318 I don't consider it a work around. I want to know everything that's possible and then decide how to use it ;] – ryanve Dec 04 '12 at 00:16
  • One legitimate use case might be a safety-fallback for global debugging tools like [kint-php](http://kint-php.github.io/kint/). Debugging calls sometimes slip into production and keeping a global fallback function can help with finding those errors (log the call) and preventing those calls from taking down a site. – joemaller Jun 15 '17 at 14:11
  • 1
    Another, unfortunately legitimate, use case is working with WordPress, and having to overwrite a pluggable function... – dingo_d Oct 01 '18 at 16:19

2 Answers2

37

It's possible but aside from being bad design, you will have to enclose your code within brackets for each namespace and won't be able to use namespace my_module; Instead it will have to be namespace my_module { ... }.

Example:

namespace my_module {

    function module_function()
    {
        // code
    }
}

namespace {
    // global namespace.

    function my_module()
    {
        // call your namespaced code
    }
}
ryanve
  • 50,076
  • 30
  • 102
  • 137
drew010
  • 68,777
  • 11
  • 134
  • 162
  • 3
    While it is strongly discouraged as a *coding practice* to combine multiple namespaces into the same file, there are many legit reasons a person may need to have it done this way not necessarily meaning "*bad design*", "*bad structure*" or "*bad coding*". Instead of commotion and criticism, we should explain and educate. After all, the language allows certain "*freedoms*" for a reason. http://php.net/manual/en/language.namespaces.definitionmultiple.php – Julio Marchi Dec 29 '18 at 03:28
  • 1
    I found it really helpful when trying to implement polyfills for older versions of PHP. I have a file that acts like an entry point in which it includes all other required files. It contains what I consider to be my "main" namespace. At the end of it, I write all my polyfills (such as https://www.php.net/manual/en/function.array-key-first.php) in the global namespace. – Rémi Breton Jun 19 '19 at 18:23
1

Functions defined in 'included' files have global scope.

So ( I haven't verified this ) you could put your function declaration in a small file, and include it.

Not quite what was envisaged in the question.

The other way to do it would be to create a local function, and in it use the 'global' keyword, and assign a closure to a global variable - which could then be used 'as a function' wherever you wanted it. I've certainly done this for ordinary variables, and I see no reason it should not work for closures.

david collier
  • 91
  • 1
  • 7