some excercises

This commit is contained in:
Aleksandr Shelkovin 2025-01-12 16:57:08 +03:00
parent 2ceedb75f5
commit 4da85dd434
7 changed files with 137 additions and 4 deletions

View File

@ -44,12 +44,12 @@ if (method_exists($point,'say')) {
$greeting = new Greeting; $greeting = new Greeting;
$greeting->x = 5; // $greeting->x = 5;
if (method_exists($greeting,'x')) { if (method_exists($greeting,'x')) {
echo $greeting->x; echo $greeting->x;
} }
$point->z = 5; // $point->z = 5;
echo '<pre>'; echo '<pre>';
print_r($point->listVariables()); print_r($point->listVariables());
@ -64,3 +64,51 @@ echo $rainbow->red;
echo "</br>"; echo "</br>";
echo $rainbow->unknown; echo $rainbow->unknown;
echo "</br>"; echo "</br>";
// require_once ('object.php');
// $objectConf = new Config;
// $objectConf->title = 'Название сайта';
// $objectConf->keywords = ['PHP', 'Phyton', 'Ruby', 'JavaScript' ];
// $objectConf->per_page = 20;
// echo '<pre>';
// print_r( $objectConf );
// echo '</pre>';
require_once ('settings.php');
$settings = new Settings;
$settings->title = 'Название сайта';
$settings->keywords = ['PHP', 'Phyton', 'Ruby', 'JavaScript' ];
$settings->per_page = 20;
echo '<pre>';
print_r( $settings );
echo '</pre>';
require_once ('static_greeting.php');
echo StaticGreeting::say('Kitty');
require_once ('page.php');
Page::render();
require_once ('static.php');
for ($objs = [], $i = 0; $i < 6; $i++) {
$objs[$i] = new Counter();
}
echo "Сейчас существует {$objs[0]->getCount()} объектов.<br/>";
$objs[5] = null;
echo"А теперь - {$objs[0]->getCount()} объекстов.<br/>";
$objs = [];
echo 'Под конец осталось - '. Counter::getCount() .' Объектов. <br/><br/>';

View File

@ -0,0 +1,5 @@
<?php
class Config {
}

View File

@ -0,0 +1,23 @@
<?php
class Page
{
static $content = 'about<br/>';
public static function footer()
{
return 'footer<br/>';
}
public static function header()
{
return 'header<br/>';
}
public static function render()
{
echo self::header() .
self::$content .
self::footer();
echo '<br/>';
}
}

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, $value): void
{
$this->properties[$key] = $value;
}
public function list(): array
{
return $this->properties;
}
}

View File

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

View File

@ -0,0 +1,9 @@
<?php
class StaticGreeting
{
public static function say(string $who) :string
{
return "Hello, $who!</br></br>";
}
}

View File

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