Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:35:43+00:00 2026-06-11T14:35:43+00:00

I was wondering if it was possible to determine what the current namespace was

  • 0

I was wondering if it was possible to determine what the current namespace was when the function was being called. I have this function declaration:

<?php
namespace Site\Action;
function add ($hook, $function) {
    /**
     * determine the namespace this was called from because
     * __NAMESPACE__ is "site\action" :(
     */
     if (is_callable($function))
         call_user_func($function);
}
?>

And on another file:

<?php
namespace Foo;
function bar () {
}
?>

And let’s say I have this as my procedural code:

<?php
namespace Foo;
Site\Action\add('hookname', 'bar');
?>

It would make sense to assume that Bar in this case was intended to resolve as Foo\bar since that was the namespace it was called from.

That was a long explanation so again, is it possible to determine the active namespace where Site\Action\add() was called from?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:35:44+00:00Added an answer on June 11, 2026 at 2:35 pm

    What you are looking for is : ReflectionFunctionAbstract::getNamespaceName

    If you want to know where you’re coming from debug_backtrace() is your friend.

    The following should solve your puzzle:

    function backtrace_namespace() 
    {
        $trace = array();
        $functions = array_map(
            function ($v) {
                return $v['function'];
            },
            debug_backtrace()
        );
        foreach ($functions as $func) {
            $f = new ReflectionFunction($func);
            $trace[] = array(
                'function' => $func, 
                'namespace' =>  $f->getNamespaceName()
            );
        }
        return $trace;
    }
    

    Just call it from anywhere to see the backtrace.
    I modified your “procedural” code file as follows:

    namespace Foo;
    
    function bar () 
    {
        var_export(backtrace_namespace());
    }
    
    /** The unasked question: We need to use the fully qualified name currently. */
    function go() 
    {
        \Site\Action\add('hookname', 'Foo\\bar');
    }
    
    go();
    

    The Result from including this file will be the following on stdout:

    array (
        0 =>
        array (
            'function' => 'backtrace_namespace',
            'namespace' => '',
        ),
        1 =>
        array (
            'function' => 'Foo\\bar',
            'namespace' => 'Foo',
        ),
        2 =>
        array (
            'function' => 'call_user_func',
            'namespace' => '',
        ),
        3 =>
        array (
            'function' => 'Site\\Action\\add',
            'namespace' => 'Site\\Action',
        ),
        4 =>
        array (
            'function' => 'Foo\\go',
            'namespace' => 'Foo',
        ),
    )
    

    Now for bonus points the answer to the hidden question:

    How do I resolve the calling namespace to avoid using fully qualified function name as argument?

    The following will allow you to call the function as you intended:

     Site\Action\add('hookname', 'bar');
    

    Without getting the dreaded:

    Warning: call_user_func() expects parameter 1 to be a valid callback, function ‘bar’ not found or invalid function name

    So before you redesign try this on for size:

    namespace Site\Action;
    
    function add($hook, $function) 
    {
        $trace = backtrace_namespace();
        $prev = (object) end($trace);
    
        $function = "$prev->namespace\\$function";
    
        if (is_callable($function))
            call_user_func($function);
    }
    

    I see no reason why debug_backtrace should not be used, this is what it is there for.

    nJoy!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if it possible to determine if an accepted socket connection has
I was wondering if it's possible to determine what kind of iPhone (for example)
I am wondering if its possible to have a an (||)or or (&&)and operator
Playing around with MapKit and I'm wondering ... Is it possible to determine where
I am wondering if it is possible to use XPath or XSL to determine
I was wondering if it possible to determine if the user has enabled the
I was wondering if anyone could help me with this problem: I have to
I was wondering what is the best way (if at all possible) to determine
I was wondering if it was possible to specifically determine if an Android device
I was wondering if it was possible to determine what position a key in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.