some exercises and christmass mystery
This commit is contained in:
parent
411e87503f
commit
bb2f805708
8
Koterov-php-training/methods_of_classes/greeting.php
Normal file
8
Koterov-php-training/methods_of_classes/greeting.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
class Greeting
|
||||||
|
{
|
||||||
|
public function say(string $who): string
|
||||||
|
{
|
||||||
|
return "Hello, $who!";
|
||||||
|
}
|
||||||
|
}
|
56
Koterov-php-training/methods_of_classes/index.php
Normal file
56
Koterov-php-training/methods_of_classes/index.php
Normal 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>';
|
46
Koterov-php-training/methods_of_classes/point.php
Normal file
46
Koterov-php-training/methods_of_classes/point.php
Normal 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
6
cristmass-tree/index.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
include_once ('tree.php');
|
||||||
|
|
||||||
|
$cristmassTree = new Tree(15);
|
||||||
|
$cristmassTree->drawTree();
|
25
cristmass-tree/tree.php
Normal file
25
cristmass-tree/tree.php
Normal 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(" ", $spaces) . str_repeat("*", $stars) . "</br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user