accessor testing
This commit is contained in:
parent
bb2f805708
commit
3eb8c2c346
@ -25,7 +25,7 @@ echo "</br>";
|
||||
var_dump( $point->getY());
|
||||
echo "</br></br>";
|
||||
|
||||
echo $point->distance();
|
||||
echo $point->distance;
|
||||
|
||||
echo '<pre>';
|
||||
print_r($point);
|
||||
@ -53,4 +53,14 @@ $point->z = 5;
|
||||
|
||||
echo '<pre>';
|
||||
print_r($point->listVariables());
|
||||
echo '</pre>';
|
||||
echo '</pre>';
|
||||
|
||||
require_once ('rainbow.php');
|
||||
|
||||
$rainbow = new Rainbow;
|
||||
echo $rainbow->yellow;
|
||||
echo "</br>";
|
||||
echo $rainbow->red;
|
||||
echo "</br>";
|
||||
echo $rainbow->unknown;
|
||||
echo "</br>";
|
@ -24,6 +24,15 @@ class Point
|
||||
// {
|
||||
// $this->y = $y;
|
||||
// }
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
if ($key === 'distance') {
|
||||
return sqrt($this->getX() ** 2 + $this->getY() ** 2);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public function getX(): int
|
||||
{
|
||||
return $this->x;
|
||||
@ -32,15 +41,14 @@ class Point
|
||||
{
|
||||
return $this->y;
|
||||
}
|
||||
public function distance(): float
|
||||
{
|
||||
return sqrt($this->getX() ** 2 + $this->getY() ** 2);
|
||||
}
|
||||
|
||||
public function listVariables(): array
|
||||
{
|
||||
return get_object_vars($this);
|
||||
}
|
||||
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
echo 'Вызов деструктора <br />';
|
||||
}
|
||||
}
|
23
Koterov-php-training/methods_of_classes/rainbow.php
Normal file
23
Koterov-php-training/methods_of_classes/rainbow.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class Rainbow
|
||||
{
|
||||
private const COLORS = [
|
||||
'red' => 'красный',
|
||||
'orange'=> 'ранжевый',
|
||||
'yellow'=> 'жёлтый',
|
||||
'green'=> 'зелёный',
|
||||
'blue'=> 'голубой',
|
||||
'indigo'=> 'синий',
|
||||
'violet'=> 'фиолетовый',
|
||||
];
|
||||
|
||||
public function __get(string $key): ?string
|
||||
{
|
||||
if (array_key_exists($key, Rainbow::COLORS)) {
|
||||
return Rainbow::COLORS[$key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,5 +2,5 @@
|
||||
|
||||
include_once ('tree.php');
|
||||
|
||||
$cristmassTree = new Tree(15);
|
||||
$cristmassTree = new Tree(50);
|
||||
$cristmassTree->drawTree();
|
||||
|
Loading…
Reference in New Issue
Block a user