Jump to content

Convert this code into php class


cromine87

Recommended Posts

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() . "");

?>

 

Link to comment
https://forums.phpfreaks.com/topic/276884-convert-this-code-into-php-class/
Share on other sites

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();

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.