Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Generate random IDs/strings in classes

    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:
    Code:
    <?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.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    PHP Code:
    $test = new rand(); // initialize a new instance of the class
    echo $test->GenerateRandID(); // echo the return of a class member function 

  • Users who have thanked Inigoesdr for this post:

    Juicy (11-11-2007)

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Cool, thanks much.

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You might want to read this.

    Always remember that the best information can be found at www.php.net.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •