title: How to call a function inside itself?
body: <p>I have a function that generates a key of 4 characters that has to be unique for each time. In order to do that, the function first generates a key, and then checks a database table to see if it's in use by someone else.</p>
  
  <p>If it's not in use, it returns the key, else, it calls itself again, but this causes the function to do an infinite loop, which is a no-no. Here's the whole function:</p>
  
  <pre><code>function key_generator($length = 4)
  {
      // I've subsequently left out the generating code,
      // which is not necesarry in this case
  
      $key = 'xxxx';
  
      if ($this-&gt;user_model-&gt;valid_key($key) == true)
      {
          return $key;
      }
      else
      {
          $this-&gt;key_generator(4);
      }
  }
  </code></pre>
  
  <p>What is the correct way to call the function again?</p>
  
  <p>By the way, I'm using CodeIgniter, hence <code>$this</code>.</p>