mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-07-25 17:13:08 +00:00
Глава 49: класс атрибута
This commit is contained in:
11
attributes/model.php
Normal file
11
attributes/model.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
spl_autoload_register();
|
||||
|
||||
$reflect = new ReflectionClass(Model\User::class);
|
||||
|
||||
echo '<pre>';
|
||||
foreach ($reflect->getAttributes() as $attribute) {
|
||||
echo $attribute->getName() . PHP_EOL;
|
||||
echo $attribute->newInstance()->info() . PHP_EOL;;
|
||||
}
|
||||
echo '</pre>';
|
23
attributes/model/user.php
Normal file
23
attributes/model/user.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Model;
|
||||
use Attribute;
|
||||
|
||||
#[Model]
|
||||
class User
|
||||
{
|
||||
}
|
||||
|
||||
#[Attribute]
|
||||
class Model
|
||||
{
|
||||
private const INFO = <<<TEXT
|
||||
Классы моделей предназначены для представления содержимого базы данных
|
||||
TEXT;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function info()
|
||||
{
|
||||
return self::INFO;
|
||||
}
|
||||
}
|
13
attributes/model_filter.php
Normal file
13
attributes/model_filter.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
spl_autoload_register();
|
||||
use Model\User as User;
|
||||
use Model\Model as Model;
|
||||
|
||||
$reflect = new ReflectionClass(User::class);
|
||||
|
||||
echo '<pre>';
|
||||
foreach ($reflect->getAttributes(Model::class) as $attribute) {
|
||||
echo $attribute->getName() . PHP_EOL;
|
||||
echo $attribute->newInstance()->info() . PHP_EOL;;
|
||||
}
|
||||
echo '</pre>';
|
Reference in New Issue
Block a user