Jump to content

Convert this code into php class


cromine87
Go to solution Solved by akphidelt2007,

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
Share on other sites

  • Solution

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();
Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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