cromine87 Posted April 12, 2013 Share Posted April 12, 2013 I'm very new at php and need help converting this random name generator code into a php class. This is the current php code: <?php$vowels = array("a", "e", "o", "u");$consonants = array("b", "c", "d", "v", "g", "t");function randVowel(){ global $vowels; return $vowels[array_rand($vowels, 1)];}function randConsonant(){ global $consonants; return $consonants[array_rand($consonants, 1)];}echo ucfirst("" . randConsonant() . "" . randVowel() . "" . "" . randConsonant() . "" . randVowel() . "" . randVowel() . "");?> Quote Link to comment https://forums.phpfreaks.com/topic/276884-convert-this-code-into-php-class/ Share on other sites More sharing options...
Solution akphidelt2007 Posted April 13, 2013 Solution Share Posted April 13, 2013 Why do you need a class for this? But, it could look something like this... class randomName { private $vowels = array("a", "e", "o", "u"); private $consonants = array("b", "c", "d", "v", "g", "t"); private function randVowel(){ return $this->vowels[array_rand($this->vowels, 1)]; } private function randConsonant() { return $this->consonants[array_rand($this->consonants, 1)]; } public function generate() { return ucfirst(self::randConsonant().self::randVowel().self::randConsonant().self::randVowel().self::randVowel()); } } $newName = new randomName(); echo $newName->generate(); Quote Link to comment https://forums.phpfreaks.com/topic/276884-convert-this-code-into-php-class/#findComment-1424489 Share on other sites More sharing options...
requinix Posted April 13, 2013 Share Posted April 13, 2013 (edited) You could go a bit fancier by, say, letting the calling code dictate the structure of the name, and using the class more of a random name generator than simply a name. And why stop at specifying the structure? class RandomNameGenerator { const DEFAULT_CONSONANTS = "bcdfghjklmnpqrstvwxyz"; const DEFAULT_VOWELS = "aeiou"; private $consonants = array(); private $structure = array(); private $vowels = array(); public function __construct($structure, $vowels = self::DEFAULT_VOWELS, $consonants = self::DEFAULT_CONSONANTS) { $this->consonants = str_split($consonants); $this->structure = str_split(strtolower($structure)); $this->vowels = str_split($vowels); } public function generate($n = 1) { $names = array(); for($i = 0; $i < $n; $i++) { $name = ""; foreach($this->structure as $type) { switch ($type) { case "c": $name .= $this->consonants[array_rand($this->consonants)]; break; case "v": $name .= $this->vowels[array_rand($this->vowels)]; break; } } $names[] = $name; } return ($n == 1 ? $names[0] : $names); } } $rng = new RandomNameGenerator("cvcvv", "aeou", "bcdvgt"); foreach($rng->generate(10) as $name) { echo "Random name: ", ucfirst($name), "\n"; }[edit] Oodles of room for more ideas to add in: first and last names, multiple types of alphabets besides consonants and vowels, random lengths and/or structures... Edited April 13, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276884-convert-this-code-into-php-class/#findComment-1424492 Share on other sites More sharing options...
cromine87 Posted April 15, 2013 Author Share Posted April 15, 2013 Theses are both great ideas of how to do this thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/276884-convert-this-code-into-php-class/#findComment-1424773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.