some exercises and christmass mystery

This commit is contained in:
Aleksandr Shelkovin 2025-01-09 20:23:47 +03:00
parent 411e87503f
commit bb2f805708
12 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
class Greeting
{
public function say(string $who): string
{
return "Hello, $who!";
}
}

View File

@ -0,0 +1,56 @@
<?php
require_once 'greeting.php';
$object = new Greeting;
echo $object->say('PHP');
echo "</br></br>";
require_once 'point.php';
$point = new Point(3, 5);
// $point->__construct();
// $point->inner();
echo "</br>";
var_dump( $point->getX());
echo "</br>";
var_dump( $point->getY());
echo "</br></br>";
// $point->setX(5);
// $point->setY(7);
var_dump( $point->getX());
echo "</br>";
var_dump( $point->getY());
echo "</br></br>";
echo $point->distance();
echo '<pre>';
print_r($point);
echo '</pre>';
// echo $point->x;
// echo '</br>';
echo '<pre>';
print_r(get_class_methods($point));
echo '</pre>';
if (method_exists($point,'say')) {
echo $point->say('PHP');
}
$greeting = new Greeting;
$greeting->x = 5;
if (method_exists($greeting,'x')) {
echo $greeting->x;
}
$point->z = 5;
echo '<pre>';
print_r($point->listVariables());
echo '</pre>';

View File

@ -0,0 +1,46 @@
<?php
class Point
{
// private $x;
// private $y;
public function __construct(private int $x = 0, private int $y = 0)
{
echo 'Вызов конструктора <br />';
$this->x = $x;
$this->y = $y;
}
public function inner()
{
$this->__construct();
}
// 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;
}
public function getY(): int
{
return $this->y;
}
public function distance(): float
{
return sqrt($this->getX() ** 2 + $this->getY() ** 2);
}
public function listVariables(): array
{
return get_object_vars($this);
}
}

6
cristmass-tree/index.php Normal file
View File

@ -0,0 +1,6 @@
<?php
include_once ('tree.php');
$cristmassTree = new Tree(15);
$cristmassTree->drawTree();

25
cristmass-tree/tree.php Normal file
View File

@ -0,0 +1,25 @@
<?php
class Tree
{
public $height;
public function __construct(int $height)
{
if ($height <= 3) {
throw new InvalidArgumentException("Your christmas tree is too small, try again!");
}
$this->height = $height;
}
public function drawTree(): void
{
$width = 2 * $this->height - 1;
for ($i = 1; $i <= $this->height; $i++) {
$stars = 2 * $i - 1;
$spaces = ($width - $stars);
echo str_repeat("&nbsp;", $spaces) . str_repeat("*", $stars) . "</br>";
}
}
}