accessor testing

This commit is contained in:
Aleksandr Shelkovin 2025-01-10 11:29:52 +03:00
parent bb2f805708
commit 3eb8c2c346
4 changed files with 49 additions and 8 deletions

View File

@ -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>";

View File

@ -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 />';
}
}

View 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;
}
}
}

View File

@ -2,5 +2,5 @@
include_once ('tree.php');
$cristmassTree = new Tree(15);
$cristmassTree = new Tree(50);
$cristmassTree->drawTree();