Created
January 30, 2021 22:39
-
-
Save BlackScorp/26248a26bfc012fbf620956491f8398c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/Person.php'; //Bauplan einbinden | |
$person1 = new Person('Viitalij Mik',new DateTime('1986-11-22')); | |
$person2 = new Person('Rasmus Lehrdorf',new DateTime('1968-11-22')); | |
echo $person1.'<br/>'; | |
echo $person2.'<br/>'; | |
echo 'Insgesammt haben wir hier '.Person::zeigePersonenAnzahl().' Personen'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person | |
{ | |
/** | |
* Eigenschaften | |
*/ | |
public string $name = ''; | |
public ?string $middleName = null; | |
public int $alter = 0; | |
public DateTime $geburtsdatum; | |
private static int $counter = 0; | |
const IQ = 140; | |
/** | |
* Methoden | |
*/ | |
public function __construct(string $name, | |
DateTime $geburtsdatum, | |
?string $middleName = null) | |
{ | |
$this->name = $name; | |
if($middleName){ | |
$this->middleName = $middleName; | |
} | |
$this->geburtsdatum = $geburtsdatum; | |
$this->rechneAlter(); | |
self::count();//rufe statische Methode auf | |
} | |
private static function count():void{ | |
self::$counter++; //setze Statische Eigenschaft | |
} | |
//Nur diese Methode kann man nutzen | |
public static function zeigePersonenAnzahl():int{ | |
return self::$counter; | |
} | |
private function rechneAlter():void | |
{ | |
//variable $heute erstellt | |
$heute = new DateTime(); | |
//variable $unterschied erstellt | |
$unterschied = $heute->diff($this->geburtsdatum); | |
$this->alter = $unterschied->y; | |
} | |
public function __toString() | |
{ | |
return 'Hi mein Name ist '.$this->name.' und ich bin '.$this->alter.' Jahre alt'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment