add oop
This commit is contained in:
		
					parent
					
						
							
								642491a9c0
							
						
					
				
			
			
				commit
				
					
						9ffda1656d
					
				
			
		
					 4 changed files with 69 additions and 0 deletions
				
			
		
							
								
								
									
										37
									
								
								demo/oop/Frage.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								demo/oop/Frage.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,37 @@ | |||
| <?php | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| readonly class Frage implements Stringable | ||||
| { | ||||
|     public function __construct( | ||||
|         private string $frage, | ||||
|         private string $richtigeAntwort, | ||||
|         private array $antworten, | ||||
|         private string $frageNr, | ||||
|     ) { | ||||
|     } | ||||
| 
 | ||||
|     public function getFrageNr(): string | ||||
|     { | ||||
|         return $this->frageNr; | ||||
|     } | ||||
| 
 | ||||
|     public function istRichtig(string $antwort): bool | ||||
|     { | ||||
|         return $antwort === $this->richtigeAntwort; | ||||
|     } | ||||
| 
 | ||||
|     public function __toString(): string | ||||
|     { | ||||
|         $fragen = ''; | ||||
| 
 | ||||
|         foreach ($this->antworten as $antwort) { | ||||
|             $fragen .= '<br>'; | ||||
|             $fragen .= sprintf('<input type="radio" name="%s" value="%s">%s</input>', $this->frageNr, $antwort, $antwort); | ||||
|         } | ||||
| 
 | ||||
|         $fragen .= '<hr>'; | ||||
| 
 | ||||
|         return $this->frage . '<br>' . $fragen; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								demo/oop/holeFragen.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/oop/holeFragen.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | |||
| <?php | ||||
| 
 | ||||
| require 'Frage.php'; | ||||
| 
 | ||||
| return [ | ||||
|     new Frage('Welches Tier bellt?', 'Hund', ['Hund', 'Katze', 'Biene'], 'frage-1'), | ||||
|     new Frage('Welche Farbe ist der Himmel bei gutem Wetter?', 'Blau', ['Rot', 'Grün', 'Blau'], 'frage-2'), | ||||
|     new Frage('Welche Frucht ist Gelb?', 'Banane', ['Banane', 'Birne', 'Kiwi'], 'frage-3'), | ||||
| ]; | ||||
							
								
								
									
										12
									
								
								demo/oop/index.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								demo/oop/index.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | |||
| <h1>fragen</h1> | ||||
| <form method="post" action="loesung.php"> | ||||
|     <?php | ||||
|     $fragen = require 'holeFragen.php'; | ||||
|     foreach ($fragen as $frage) { | ||||
|         echo $frage; | ||||
|     } | ||||
|     ?>
 | ||||
| 
 | ||||
|     <input type="submit" value="Absenden"> | ||||
| </> | ||||
| 
 | ||||
							
								
								
									
										11
									
								
								demo/oop/loesung.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								demo/oop/loesung.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | |||
| <?php | ||||
| 
 | ||||
| $fragen = require 'holeFragen.php'; | ||||
| foreach ($fragen as $frage) { | ||||
|     if ($frage->istRichtig($_POST[$frage->getFrageNr()])) { | ||||
|         echo $frage->getFrageNr() . ' ist Richtig'; | ||||
|     } else { | ||||
|         echo $frage->getFrageNr() . ' ist Falsch'; | ||||
|     } | ||||
|     echo "<hr>"; | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue