note 123875 deleted from language.oop5.decon by cmb
| From: | cmb@php.net | Date: | Wed, 22 May 2019 21:19:12 +0000 |
| Subject: | note 123875 deleted from language.oop5.decon by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211110@lists.php.net to get a copy of this message | ||
Note Submitter: doe22568 at cndps dot com
----
Extension to OOP can work with using a constructor -> calculated age
[Document user.php]
<?php
class User
{
//Attribute
private $name;
private $forename;
private $birthday;
private $username;
//Konstruktor
function __construct($n, $fn, $birthday)
{
if(strlen($n)>=3){$this->name=$n;}
if(strlen($fn)>=2){$this->forename=$fn;}
$this->birthday=$birthday;
}
//Methoden
public function setName($n)
{
$this->name=$n;
}
public function getName()
{
if($this->name!=""){return $this->name;}
else return "<strong style='color:red'>Name required!</strong>";
}
public function getForename()
{
if($this->forename!=""){return $this->forename;}
else return "<strong style='color:red'>Forename
required!</strong>";
}
public function getBirthday()
{
if($this->birthday!=""){return $this->birthday;}
else return "<strong style='color:red'>Birthday
required!</strong>";
}
public function getUsername()
{
if($this->username!="")
{
return $this->username;
}
else if($this->name!="" AND $this->forename!="")
{
$this->createUN();
return $this->username;
}
else
{
return "<strong style='color:red'>No Username!</strong>";
}
}
private function createUN()
{
$i=0;
$un=array();
while($i < 3)
{
$un[$i]=$this->name[$i];
$i++;
}
$i=0;
while($i < 2)
{
$un[3+$i]=$this->forename[$i];
$i++;
}
$un=implode($un);
$un=strtoupper($un);
$this->username=$un;
}
private function CalculateAge()
{
if($this->birthday!="")
{
$date=$this->birthday;
$date=explode(".",$date);
$myDay=$date[0];
$myMonth=$date[1];
$myYear=$date[2];
$day=date("d");
$month=date("m");
$year=date("Y");
$age=$year-$myYear;
if($myMonth > $month)
{
return $age-=1;
}
else
{
if($myMonth == $month AND $myDay > $day)
{
return $age-=1;
}
else
{
return $age;
}
}
}
else return "<strong style='color:red'>?</strong>";
}
public function Output()
{
echo"
<h1>Your data for control</h1>
<div class='output'>Your Name: <strong
style='color:green'>".$this->getName()."</strong></div>
<div class='output'>Your Forename: <strong
style='color:green'>".$this->getForename()."</strong></div>
<div class='output'>Your Birthday: <strong
style='color:green'>".$this->getBirthday()."</strong></div>
<div class='output'>Your age <strong
style='color:green'>".$this->CalculateAge()."</strong> years
old.</div>
<div class='output'>Username: <strong
style='color:green'>".$this->getUsername()."</strong></div>
";
}
//////////////////////////////////////////////////
[Document user_test.php]
<html>
<form method="POST">
<div class="output">
<label for="lfn">enter forenamen</label>
<input type="text" id="lfn" name="fn">
</div>
<div class="output">
<label for="lln">enter last name</label>
<input type="text" id="lln" name="ln">
</div>
<div class="output">
<label for="lb">enter birthday</label>
<input type="text" id="lb" name="b">
</div>
<input type="submit" value="Enter">
<?php
include("username.php");
if(!empty($_POST))
{
$bn1=new User($_POST["ln"],$_POST["fn"],$_POST["b"]);
echo $bn1->Output();
}
?>
</html>