title: PHP namespace removal / mapping and rewriting identifiers
body: <p>I'm attempting to automate the removal of namespaces from a PHP class collection to make them PHP 5.2 compatible. (Shared hosting providers do not fancy rogue PHP 5.3 installations. No idea why. Also the code in question doesn't use any 5.3 feature additions, just that syntax. Autoconversion seems easier than doing it by hand or reimplementing the codebase.)</p>
  
  <p>For rewriting the *.php scripts I'm basically running over a <a href="http://php.net/token_get_all" rel="noreferrer">tokenizer</a> list. The identifier searching+merging is already complete. But I'm a bit confused now how to accomplish the actual rewriting. </p>
  
  <pre><code>function rewrite($name, $namespace, $use) {
  
      global $identifiers2;            // list of known/existing classes
  
      /*
          bounty on missing code here
      */
  
      return strtr($name, "\\", "_");  // goal: backslash to underscore
  }
  </code></pre>
  
  <p>That function is going to be invoked on each found identifier (whether class, function or const). It will receive some context information to transform a local identifier into an absolute/global $name:</p>
  
  <pre><code>$name =
      rewrite(
          "classfuncconst",      # &lt;-- foreach ($names as $name)
          "current\name\space",
          array(
             'namespc' =&gt; 'use\this\namespc',
             'alias' =&gt; 'from\name\too',
             ...
          )
      );
  </code></pre>
  
  <p>At this stage I've already prepared an <code>$identifiers2</code> list. It contains a list of all known classes, functions and constant names (merged for simplicity here). </p>
  
  <pre><code>$identifiers2 = array(             // Alternative suggestions welcome.
     "name\space\Class" =&gt; "Class",  // - list structure usable for task?
     "other\ns\func1" =&gt; "func1",    // - local name aliases helpful?
     "blip\CONST" =&gt; "CONST",        // - (ignore case-insensitivity)
  </code></pre>
  
  <p>The <code>$name</code> parameter as received by the <code>rewrite()</code> function can be a <strong>local</strong>, <strong>unqualified</strong>, <strong>\absolute</strong> or <strong>name\spaced</strong> identifier (but just identifers, no expressions). The <code>$identifiers2</code> list is crucial to resolve <em>unqualified</em> identifiers, which can refer to things in the current namespace, or if not found there, global stuff.</p>
  
  <p>And the various <code>use namespace</code> aliases have to be taken into account and add some complication besides the namespace resolving and precedence rules.  </p>
  
  <p>So, how / in which order would you attempt to convert the variations of class/function names here?</p>
  
  <blockquote>
    <h3>Mental Laziness Bounty.</h3>
    
    <p>To make this a less blatant plzsendtehcodez question: an explainative instruction list or pseudo-code answer would be eligible too. And if another approach would be more suitable for the task, please elaborate on that rather. (But no, upgrading PHP or changing the hoster is not an option.)</p>
    
    <p>I think I've figured it out meanwhile, but the question is still open for answers / implementation proposals. (Otherwise the bounty will obviously go to nikic.)</p>
  </blockquote>