mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-07-25 17:13:08 +00:00
Глава 14: аксессоры
This commit is contained in:
13
methods/config.php
Normal file
13
methods/config.php
Normal 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>';
|
@ -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
30
methods/distance.php
Normal 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
5
methods/distance_use.php
Normal 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
21
methods/rainbow.php
Normal 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
7
methods/rainbow_use.php
Normal 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
23
methods/settings.php
Normal 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
12
methods/settings_use.php
Normal 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
31
methods/static.php
Normal 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 />';
|
Reference in New Issue
Block a user