2

我想在类中创建一个函数来创建用户名,函数将检查用户名是否存在,然后它将增加用户名,如 username_1。并检查此用户名是否存在,如果存在,则再次将其增加到 username_2 直到创建新用户名。我已经创建了这个函数,但它什么也没给我。请帮我看看我的代码有什么问题。

class a{

function check_username($username){


  if($usernameexist){

    return true;
  }
  else
  {

   return false;

  }

}

function create_username($username) {

        $__name = __FUNCTION__;

        if ($this->check_username($username)) {                
            $n++;
            $username = $username . "_" . $n;
            //return $__name($username);  this return fatal error.
            return call_user_func('create_username', $username);

        } else {
            return $username;               
        }
    }
}
4

3 回答 3

3

不需要为此使用递归,一个简单的while(){}循环就可以了:

Plain-Jane Interator 方法

// your original function
function create_username($username){
  // check if the username (as-is) already exists
  if ($this->check_username($username)){
    // use $n to keep a counter
    $n = 1;
    // while {username}_{n} exists, keep incrementing the counter
    while ($this->check_username($username.'_'.$n)){
      $n++;

      /* If you don't want this to check to infinity, uncomment
       * the below portion. the 100 is an arbitrary number, but use
       * whatever you want as a limitation (could even make it a
       * parameter in the method). Also, returning FALSE allows you to
       * gracefully catch when max attempts are reached.
       *
       * e.g.
       *   if (($new_user = $obj->create_username('BradChristie')) !== FALSE){
       *     // user was successfully created within the max allowed attempts
       *   }
       */
      //if ($n > 100) return FALSE
    }
    // return the result
    return $username.'_'.$n;
  }
  // username was fine, return it back
  return $username;
}

递归方法

// recursive username check
public function create_username($username, $n = 0)
{
  /* Same as above function, this is a check to prevent counting
   * to infinity. uncomment to apply it
   */
  //if ($n > 100) return FALSE;

  // establish the username we're testing. if $n is 0,
  // it's the original call to the function (don't add _0)
  // if it's >0, it's part of the search so include it
  $_username = $username . ($n > 0 ? '_'.$n : '');

  // check if the username exists.
  if ($this->check_username($_username))
  {
    // it exists, so make a call to this same function passing
    // the original username and the value of n + 1 (move to next
    // possibility)
    return $this->create_username($username, $n+1);
  }

  // the name, as-is, was fine. return it
  return $_username;
}

例子

于 2013-01-16T13:30:14.943 回答
0

在这种情况下不需要递归......一个简单的循环就可以完美地完成:

function create_username($username) {

    $original_username = $username;
    $i=1;
    while(! $this->check_username($username) ) {
        $username = $original_username . '_' .$i++;
    }

    return $username;
}
于 2013-01-16T13:26:36.797 回答
0

您的代码在几个方面是错误的,正如在其他地方指出的那样,您想要的函数最好迭代编写。

您的代码的一些问题如下:

  1. check_username成功时,您正在执行递归检查。所以,如果你找不到原始$username文件,你永远不会修改它,所以永远不要检查修改后的值。
  2. 您正在create_username通过附加_n(对于适当的n)来修改传递给的名称。由于您在递归调用中传递了修改后的名称,因此您实际上最终会_n在名称上包含多个部分。
  3. 由于您没有限制递归调用,即使编写正确,您最终也会嵌套太深。
于 2013-01-16T13:35:20.283 回答