public function generateEmailLocalPart() { $local_parts_to_try = $this->get_all_possible_local_part(); $counter = 0; $local_part_to_try = array_shift($local_parts_to_try); while (!UserTable::check_if_local_part_exists($local_part_to_try)) { if (count($local_parts_to_try) == 0) { $counter++; $local_parts_to_try = $this->get_all_possible_local_part($counter); } $local_part_to_try = array_shift($local_parts_to_try); if ($counter == 3) { die('Too many attempts to find a local part to try.'); } } $this->setEmailLocalPart($local_part_to_try); return $local_part_to_try; }