...

View Full Version : Generate random IDs/strings in classes



Juicy
11-11-2007, 07:50 PM
Ok, so I've started using php and I am having some problems understanding how classes work. I think the code I have thus far is good, but I am unsure of how to actually call the class functions and make it all work.

Here is what I have so far:

<?php
class rand
{

function GenerateRandID()
{

return md5($this->GenerateRandStr(16));

}


function GenerateRandStr($length)
{

$randstr = "";

for($i=0; $i<$length; $i++)
{

$randnum = mt_rand(0,61);

if($randnum < 10)
{

$randstr .= chr($randnum+48);

}
else if($randnum < 36)
{

$randstr .= chr($randnum+55);

}
else
{

$randstr .= chr($randnum+61);

}

}

return $randstr;

}
}
?>

So, do I need some sort of var declarations at the start of the class? How can I use this code to actually generate a random string? This is the first time I have tried using object oriented code in php so forgive my lack of skill.

Inigoesdr
11-11-2007, 07:54 PM
$test = new rand(); // initialize a new instance of the class
echo $test->GenerateRandID(); // echo the return of a class member function

Juicy
11-11-2007, 07:58 PM
Cool, thanks much.

aedrin
11-12-2007, 04:02 PM
You might want to read this (http://www.php.net/zend-engine-2.php).

Always remember that the best information can be found at www.php.net (http://www.php.net).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum