什么是在php中递归调用函数的正确语法 [英] What is correct syntax for calling a function recursively in php

查看:109
本文介绍了什么是在php中递归调用函数的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用递归来调用自己的函数,我需要知道调用自己的正确语法。



注意:我正在使用面向对象编程技术并且该函数来自一个类文件。



以下是我的函数

  //生成唯一的激活码
// *********************************** **************************************
public function generateUniqueActivationCode()
{
$ mysql = new Mysql();
$ string = new String();

$ activation_code = $ string-> generateActivationCode();

//激活码是否唯一检查
$ sql =SELECT activation_id FROM。 TABLE_ACTIVATION_CODES。WHERE activation_code ='$ activation_code'LIMIT 1;
$ query = $ mysql-> query($ sql);
$ b $ if($ mysql-> rowCount($ query)> 0)
{
//此函数以递归方式调用自身
return generateUniqueActivationCode(); //< - 这个语法在Oops
}中是否正确
else
{
return $ activation_code;




$ b

如果递归调用它的代码是

  return generateUniqueActivationCode(); 

OR

  return $ this-> generateUniqueActivationCode(); 

或者其他方式以外的其他方式。



请让我知道。

解决方案

递归是完全错误的解决这个问题的方法



与迭代不同的是,您正在填充堆栈,并且不必要地生成新对象。 正确的方法解决问题的方法是在一个范围内生成一个随机值,使得重复的可能性很小,但是如果没有外部量词(例如用户名)来定义范围,那么迭代就是一种方法。



您的代码还存在其他问题 - 您应该在查看记录的相同位置添加记录。


我使用的是面向对象编程技术,函数来自类文件

然后它不是一个函数,它是一个方法。



而且你的代码对SQL注入很不利。



A更好的解决方案是:

  class xxxx {
....
public function generateUniqueActivationCode($ id )
{
if(!$ this-> mysql)$ this-> mysql = new Mysql();
if(!$ this-> string)$ this-> string = new String();
$ limit = 10;

do {
$ activation_code = $ string-> generateActivationCode();
$ ins = mysql_escape_string($ activation_code);
$ sql =INSERT INTO。 TABLE_ACTIVATION_CODES。(activation_id,activation_code)
。 VALUES($ id,'$ ins);
$ query = $ mysql-> query($ sql);
if(stristr($ query-> error(),'duplicate')){
continue;
}
返回$ query-> error()? false:$ activation_code;
} while(limit--);
返回false;
}

} //结束课程


I have a function which is using recursion to call itself and I need to know the correct syntax for calling itself.

Note: I am using Object oriented programming technique and the function is coming from a class file.

Below is my function

// Generate Unique Activation Code
//*********************************************************************************
    public function generateUniqueActivationCode()
    {
        $mysql = new Mysql();
        $string = new String();

        $activation_code = $string->generateActivationCode();

        // Is Activation Code Unique Check
        $sql = "SELECT activation_id FROM ". TABLE_ACTIVATION_CODES ." WHERE activation_code='$activation_code' LIMIT 1";
        $query = $mysql->query($sql);

        if($mysql->rowCount($query) > 0)
        {
            // This function is calling itself recursively
            return generateUniqueActivationCode(); // <- Is this syntax correct in Oops
        }
        else
        {
            return $activation_code;
        }
    }

Should the code to call it recursively be

return generateUniqueActivationCode();

OR

return $this->generateUniqueActivationCode();

or if something else other than these 2 ways.

Please let me know.

解决方案

Recursion is the COMPLETELY WRONG WAY TO SOLVE THIS PROBLEM

Unlike iteration you're filling up the stack, and generating new objects needlessly.

The right way to solve the problem is to generate a random value within a scope which makes duplicates very unlikely, however without some external quantifier (such as a username) to define the scope then iteration is the way to go.

There are further issues with your code - really you should be adding records in the same place where you check for records.

I am using Object oriented programming technique and the function is coming from a class file

Then it's not a function, it's a method.

And your code is susceptibale to SQL injection.

A better solution would be:

class xxxx {
....
public function generateUniqueActivationCode($id)
{
   if (!$this->mysql) $this->mysql = new Mysql();
   if (!$this->string) $this->string = new String();
   $limit=10;

   do {
      $activation_code = $string->generateActivationCode();
      $ins=mysql_escape_string($activation_code);
      $sql="INSERT INTO ". TABLE_ACTIVATION_CODES ." (activation_id, activation_code)"
          . "VALUES ($id, '$ins)";
      $query = $mysql->query($sql);
      if (stristr($query->error(), 'duplicate')) {
         continue;
      }
      return $query->error() ? false : $activation_code;
   } while (limit--);
   return false;
}

} // end class

这篇关于什么是在php中递归调用函数的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆