29

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()
6
  • 2
    You probably shouldn't do this, as it cries "bad design, bad structure" all over. Your particular reason to do so would be? Commented Dec 3, 2012 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. Commented Dec 3, 2012 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. Commented Dec 4, 2012 at 0: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 ;] Commented Dec 4, 2012 at 0:16
  • 2
    Another, unfortunately legitimate, use case is working with WordPress, and having to overwrite a pluggable function... Commented Oct 1, 2018 at 16:19

2 Answers 2

39

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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. php.net/manual/en/language.namespaces.definitionmultiple.php
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 php.net/manual/en/function.array-key-first.php) in the global namespace.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.