Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>What you are looking for is : <a href="http://php.net/manual/en/reflectionfunctionabstract.getnamespacename.php" rel="nofollow">ReflectionFunctionAbstract::getNamespaceName</a></h2> <p>If you want to know where you're coming from <a href="http://php.net/manual/en/function.debug-backtrace.php" rel="nofollow">debug_backtrace()</a> is your friend.</p> <p>The following should solve your puzzle:</p> <pre><code>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' =&gt; $func, 'namespace' =&gt; $f-&gt;getNamespaceName() ); } return $trace; } </code></pre> <p>Just call it from anywhere to see the backtrace. I modified your "procedural" code file as follows:</p> <pre><code>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(); </code></pre> <p>The Result from including this file will be the following on stdout:</p> <pre><code>array ( 0 =&gt; array ( 'function' =&gt; 'backtrace_namespace', 'namespace' =&gt; '', ), 1 =&gt; array ( 'function' =&gt; 'Foo\\bar', 'namespace' =&gt; 'Foo', ), 2 =&gt; array ( 'function' =&gt; 'call_user_func', 'namespace' =&gt; '', ), 3 =&gt; array ( 'function' =&gt; 'Site\\Action\\add', 'namespace' =&gt; 'Site\\Action', ), 4 =&gt; array ( 'function' =&gt; 'Foo\\go', 'namespace' =&gt; 'Foo', ), ) </code></pre> <p>Now for bonus points the answer to the hidden question:</p> <h2>How do I resolve the calling namespace to avoid using fully qualified function name as argument?</h2> <p>The following will allow you to call the function as you intended: </p> <pre><code> Site\Action\add('hookname', 'bar'); </code></pre> <p>Without getting the dreaded:</p> <blockquote> <p>Warning: call_user_func() expects parameter 1 to be a valid callback, function 'bar' not found or invalid function name </p> </blockquote> <p>So before you redesign try this on for size:</p> <pre><code>namespace Site\Action; function add($hook, $function) { $trace = backtrace_namespace(); $prev = (object) end($trace); $function = "$prev-&gt;namespace\\$function"; if (is_callable($function)) call_user_func($function); } </code></pre> <p>I see no reason why <code>debug_backtrace</code> should not be used, this is what it is there for.</p> <p>nJoy!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload