0

i am new comer for php, why this program give us cll to undefined function error with php.

<?php
class Ksb_CodeGenerator{

public function get_rand_id($length)
{
  if($length>0) 
  { 
  $rand_id="";
   for($i=1; $i<=$length; $i++)
   {
   mt_srand((double)microtime() * 1000000);
   $num = mt_rand(1,36);
   $rand_id .= assign_rand_value($num);
   }
  }
return $rand_id;
} 

 public function assign_rand_value($num)
{
// accepts 1 - 36
  switch($num)
  {
    case "1":
     $rand_value = "a";
    break;
    case "2":
     $rand_value = "b";
    break;
    case "3":
     $rand_value = "c";
    break;
    case "4":
     $rand_value = "d";
    break;
    case "5":
     $rand_value = "e";
    break;
    case "6":
     $rand_value = "f";
    break;
    case "7":
     $rand_value = "g";
    break;
    case "8":
     $rand_value = "h";
    break;
    case "9":
     $rand_value = "i";
    break;
    case "10":
     $rand_value = "j";
    break;
    case "11":
     $rand_value = "k";
    break;
    case "12":
     $rand_value = "l";
    break;
    case "13":
     $rand_value = "m";
    break;
    case "14":
     $rand_value = "n";
    break;
    case "15":
     $rand_value = "o";
    break;
    case "16":
     $rand_value = "p";
    break;
    case "17":
     $rand_value = "q";
    break;
    case "18":
     $rand_value = "r";
    break;
    case "19":
     $rand_value = "s";
    break;
    case "20":
     $rand_value = "t";
    break;
    case "21":
     $rand_value = "u";
    break;
    case "22":
     $rand_value = "v";
    break;
    case "23":
     $rand_value = "w";
    break;
    case "24":
     $rand_value = "x";
    break;
    case "25":
     $rand_value = "y";
    break;
    case "26":
     $rand_value = "z";
    break;
    case "27":
     $rand_value = "0";
    break;
    case "28":
     $rand_value = "1";
    break;
    case "29":
     $rand_value = "2";
    break;
    case "30":
     $rand_value = "3";
    break;
    case "31":
     $rand_value = "4";
    break;
    case "32":
     $rand_value = "5";
    break;
    case "33":
     $rand_value = "6";
    break;
    case "34":
     $rand_value = "7";
    break;
    case "35":
     $rand_value = "8";
    break;
    case "36":
     $rand_value = "9";
    break;
  }
return $rand_value;
}
}




$obj1 = new Ksb_CodeGenerator();
    print $obj1->get_rand_id(7);
?>




Fatal error: Call to undefined function assign_rand_value() in C:\wamp\www\kvch_new\ksb_codeGenerator.php on line 14

i want craete randon code but i can not create code for varification code.in above code, i am creating a object namely $obj1 and $obj1 is colling get_rand_id() function. this program is being used in code php file in acore php project.

please help me

2
  • 1
    You're in a class, accessing a local function - you need to access it as $this->assign_rand_value() Commented Oct 12, 2013 at 11:40
  • Please read php.net/manual/en/language.oop5.php Commented Oct 12, 2013 at 11:42

2 Answers 2

2

replace

$rand_id .= assign_rand_value($num);

with

$rand_id .= $this->assign_rand_value($num);

because if you call function like assign_rand_value($num) this meaning it will call either php function or core function you have created or included.

And doing $this->assign_rand_value($num) will call the class member function.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't you think, that as a 10k+ user, you should be answering questions that are beyond "help, I cannot read manual" level?
1

You should write:

   $rand_id .= $this->assign_rand_value($num);

Otherwise you could do something like this:

$options = array_merge(range('a', 'z'), range('0', '9'));
$rand_id.= $options[$num];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.