Глава 49: завершение черновика

This commit is contained in:
Igor Simdyanov
2022-06-25 17:01:46 +03:00
parent ef19283c2c
commit d0b82f0067
3 changed files with 47 additions and 1 deletions

View File

@ -20,4 +20,4 @@ class Model
{
return self::INFO;
}
}
}

25
attributes/my_model.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace MyModel;
require_once('my_model/my_model.php');
#[MyModel(['id' => 'news_id', 'body' => 'content'])]
#[MyModel(info: 'Модель страницы')]
#[MyModel('SEO-информация')]
#[MyModel(MyModel::INFO)]
#[MyModel('Ivan' . ' ' . 'Ivanov')]
class User
{
}
$reflect = new \ReflectionClass(User::class);
echo '<pre>';
foreach ($reflect->getAttributes() as $attribute) {
$info = $attribute->newInstance()->info();
if (is_array($info)) {
print_r($info);
} else {
echo $attribute->newInstance()->info() . PHP_EOL;;
}
}
echo '</pre>';

View File

@ -0,0 +1,21 @@
<?php
namespace MyModel;
use Attribute;
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
class MyModel
{
const INFO = 'Класс для взаимодействия с базой данных';
private $info;
public function __construct($info = self::INFO)
{
$this->info = $info;
}
public function info()
{
return $this->info;
}
}