mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-07-25 17:13:08 +00:00
Глава 49: завершение черновика
This commit is contained in:
@ -20,4 +20,4 @@ class Model
|
||||
{
|
||||
return self::INFO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
attributes/my_model.php
Normal file
25
attributes/my_model.php
Normal 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>';
|
21
attributes/my_model/my_model.php
Normal file
21
attributes/my_model/my_model.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user