• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
" Undefined variable" message when I am trying to create custom library.

#1
[eluser]Unknown[/eluser]
This is my library
class Testing extends CI_Loader {

function __construct() {

function randomtxt($n){
$codelenght = $n;
while($newcode_length < $codelenght) {
$x=1;
$y=3;
$part = rand($x,$y);
if($part==1){$a=48;$b=57;} // Numbers
if($part==2){$a=65;$b=90;} // UpperCase
if($part==3){$a=97;$b=122;} // LowerCase
$code_part=chr(rand($a,$b));
$newcode_length = $newcode_length + 1;
$newcode = $newcode.$code_part;
}
return $newcode;
}
}

I am trying to call in my controller..
$this->load->library('testing');
echo $this->testing->randomtxt(12);
I am gettig fallowing error:
Message: Undefined variable: newcode_length
Please help me..
Thank you.


#2
[eluser]TWP Marketing[/eluser]
[quote author="rajender jakka" date="1344351668"]This is my library
Code:
class Testing extends CI_Loader {

    function __construct() {

function randomtxt($n){
   $codelenght = $n; // spelling: $codelength
  $newcode_length = 0; // define initial value for $newcode_length
  while($newcode_length < $codelenght) { // spelling: $codelength
  $x=1;
  $y=3;
  $part = rand($x,$y);
  if($part==1){$a=48;$b=57;}  // Numbers
  if($part==2){$a=65;$b=90;}  // UpperCase
  if($part==3){$a=97;$b=122;} // LowerCase
  $code_part=chr(rand($a,$b));
  $newcode_length = $newcode_length + 1;
  $newcode = $newcode.$code_part;
  }
  return $newcode;
}
}
I am trying to call in my controller..
$this->load->library('testing');
echo $this->testing->randomtxt(12);
I am gettig fallowing error:
Message: Undefined variable: newcode_length
Please help me..
Thank you.

[/quote]
First, enclose your code in the [ code] brackets to make it easier to read.

The problem is that you have not defined your var: $newcode_length before using it in the while statement. Assign a default value before the while(). I set it to zero as shown above.

Also, beware of the spelling, it will come back to bite you later.

#3
[eluser]Aken[/eluser]
There's also zero reason for you to extend the CI_Loader library if you're creating your own new library.

#4
[eluser]PhilTem[/eluser]
[quote author="Aken" date="1344401939"]There's also zero reason for you to extend the CI_Loader library if you're creating your own new library.[/quote]

First of all, Aken is totally right. You don't extend any class if you create a custom library.

Second: You should always initialize all variables before you first access them. CI has set error_reporting to E_ALL when in development-environment. That way, it will create errors when you access a variable that was not initialized by you prior to accessing it.
In other words:

Code:
$codelenght = $n;
$newcode_length = 0;
  while($newcode_length < $codelenght) {
...

PS: This is not an issue with CI, it's an issue with good coding style since every script should give you this error when error_reporting == E_ALL and you do something like accessing a variable before it's initialized Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.