Глава 14: аксессоры

This commit is contained in:
Igor Simdyanov
2022-05-03 11:26:20 +03:00
parent ab5e6c9f8a
commit 2779555912
9 changed files with 142 additions and 8 deletions

13
methods/config.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class Config {
}
$object = new Config;
$object->title = 'Название сайта';
$object->keywords = ['PHP', 'Python', 'Ruby', 'JavaScript'];
$object->per_page = 20;
echo '<pre>';
print_r((array)$object);
echo '<pre>';

View File

@ -9,14 +9,6 @@ class Point {
$this->y = $y;
}
public function setX(int $x) : void
{
$this->x = $x;
}
public function setY(int $y) : void
{
$this->y = $y;
}
public function getX() : int
{
return $this->x;

30
methods/distance.php Normal file
View File

@ -0,0 +1,30 @@
<?php
class Point {
private $x;
private $y;
public function __construct(int $x, int $y)
{
$this->x = $x;
$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;
}
public function getY() : int
{
return $this->y;
}
}

5
methods/distance_use.php Normal file
View File

@ -0,0 +1,5 @@
<?php
require_once('distance.php');
$point = new Point(x: 5, y: 3);
echo $point->distance; // 5.8309518948453

21
methods/rainbow.php Normal file
View File

@ -0,0 +1,21 @@
<?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;
}
}
}

7
methods/rainbow_use.php Normal file
View File

@ -0,0 +1,7 @@
<?php
require_once('rainbow.php');
$rainbow = new Rainbow;
echo $rainbow->yellow; // желтый
echo $rainbow->red; // красный
echo $rainbow->unknown; // null

23
methods/settings.php Normal file
View File

@ -0,0 +1,23 @@
<?php
class Settings {
private array $properties;
public function __get(string $key) : ?string
{
if(array_key_exists($key, $this->properties)) {
return $this->properties[$key];
} else {
return null;
}
}
public function __set(string $key, mixed $value) : void
{
$this->properties[$key] = $value;
}
public function list() : array
{
return $this->properties;
}
}

12
methods/settings_use.php Normal file
View File

@ -0,0 +1,12 @@
<?php
require_once('settings.php');
$settings = new Settings;
$settings->title = 'Название сайта';
$settings->keywords = ['PHP', 'Python', 'Ruby', 'JavaScript'];
$settings->per_page = 20;
echo '<pre>';
print_r($settings->list());
echo '</pre>';

31
methods/static.php Normal file
View File

@ -0,0 +1,31 @@
<?php
class Counter
{
private static $count = 0;
public function __construct()
{
self::$count++;
}
public function __destruct()
{
self::$count--;
}
public static function getCount() : int
{
return self::$count;
}
}
for ($objs = [], $i = 0; $i < 6; $i++) {
$objs[] = new Counter();
}
echo "Сейчас существует {$objs[0]->getCount()} объектов.<br />";
$objs[5] = null;
echo "А теперь - {$objs[0]->getCount()} объектов.<br />";
$objs = [];
echo 'Под конец осталось - ' . Counter::getCount() .' объектов.<br />';