php class Worker private name private age private salary public functi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class Worker
{
private $name;
private $age;
private $salary;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setAge($age)
{
$this->checkAge($age);
}
public function getAge()
{
return $this->age;
}
public function setSalary($salary)
{
$this->salary = $salary;
}
public function getSalary()
{
return $this->salary;
}
private function checkAge($age)
{
if ($age >= 1 && $age <= 100) {
$this->age = $age;
} else {
return false;
}
}
}
$Ivan = new Worker();
$Ivan->setAge('25');
$Ivan->setName('Иван');
$Ivan->setSalary('1000');
$Vasiliy = new Worker();
$Vasiliy->setAge('26');
$Vasiliy->setName('Вася');
$Vasiliy->setSalary('2000');
echo $Ivan->getSalary() + $Vasiliy->getSalary() . '<br>';
echo $Ivan->getAge() + $Vasiliy->getAge() . '<br>';
function showWorker(object $object)
{
if ($object instanceof Worker && method_exists($object, 'getName') && method_exists($object, 'getAge')) {
echo $object->getName() . " " . $object->getAge() . " " . "лет" . '<br>';
} else {
echo "Данный сотрудник не найден";
}
}
showWorker($Ivan);
showWorker($Vasiliy);