php - php类中的递归函数

标签 php recursion

我想在类中创建一个函数,来创建用户名,函数将检查用户名是否存在,然后它将增加用户名,例如 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;               
        }
    }
}

最佳答案

无需为此使用递归,一个简单的 while(){} 循环即可:

简单的交互器方法

// 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;
}

Example

关于php - php类中的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359431/

相关文章:

php - AbstractSmtpTransport.php 中的 Swift_TransportException 第 404 行 : Connection to smtp. gmail.com:465 超时

sql - Postgres 中带总和的递归查询

在C中的递归函数中连接字符

algorithm - Picopala算法实现

php - 使用 SQL 和 PHP 创建访问次数最多的列表?

php - 将 SNMP 陷阱数据包传递给 Ubuntu 上的 php 守护进程

php - 字符串损坏或 preg_match 错误?

MYSQL CTE 递归更新

sql - 如何在递归SQL查询中找到子树中的所有节点?

php - XPath 2.0 和/或 XSLT 2.0 会在 PHP 中实现吗?