当前位置: 首页 > wzjs >正文

外国风格网站建设电话手机中国手机大全

外国风格网站建设电话,手机中国手机大全,wordpress 文章访问次数,专业网站设计公司和普通设计公司的区别简介 PHP 8 引入了 属性(Attributes)作为新的元数据机制,用于替代传统的 PHPDoc 注解,使得代码更具类型安全性和结构化。 基本语法 PHP 8 的属性(Attributes)使用 #[...] 语法表示,并可以用于…

简介

PHP 8 引入了 属性(Attributes)作为新的元数据机制,用于替代传统的 PHPDoc 注解,使得代码更具类型安全性和结构化。

基本语法

PHP 8 的属性(Attributes)使用 #[...] 语法表示,并可以用于类、方法、属性、参数、常量等。

定义属性

属性的本质是一个 PHP 类,通常以 Attribute 特性(flag)标记:

#[Attribute] // 这是一个属性定义
class MyAttribute {public function __construct(public string $name) {}
}
不带 __construct() 的空类
#[Attribute]
class SimpleAttribute {}

使用示例:

#[SimpleAttribute]
class AnotherClass {}
使用属性

定义好 MyAttribute 之后,可以在类、方法、属性等地方使用:

#[MyAttribute("Hello World")]
class MyClass {}

属性应用范围

PHP 8 允许在不同的地方使用属性,包括:

  • 类的属性

  • 类的方法

  • 方法参数

  • 常量

应用到类
#[MyAttribute("This is a class")]
class DemoClass {}
应用到属性
class User {#[MyAttribute("User ID")]public int $id;
}
应用到方法
class MyController {#[MyAttribute("This is a method")]public function myMethod() {}
}
应用到方法参数
class Test {public function greet(#[MyAttribute("Parameter annotation")] string $name) {echo "Hello, $name";}
}
应用到类常量
class Status {#[MyAttribute("Status Active")]public const ACTIVE = 1;
}

解析属性

PHP 提供了 Reflection 机制来获取属性信息。

获取类的属性
$reflection = new ReflectionClass(MyClass::class);
$attributes = $reflection->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();echo $instance->name; // 输出: Hello World
}
获取常量的属性
$reflectionConstant = new ReflectionClassConstant(MyClass::class, 'MY_CONST');
$attributesConstant = $reflectionConstant->getAttributes();
foreach ($attributesConstant as $attribute) {$instance = $attribute->newInstance();echo $instance->name . "\n";
}
获取属性的属性
$reflection = new ReflectionProperty(User::class, 'id');
$attributes = $reflection->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();echo $instance->name;
}
获取方法的属性
$reflection = new ReflectionMethod(MyController::class, 'myMethod');
$attributes = $reflection->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();echo $instance->name;
}
获取方法参数的属性
$reflectionMethod = new ReflectionMethod(MyClass::class, 'greet');
$parameters = $reflectionMethod->getParameters();
foreach ($parameters as $parameter) {$attributes = $parameter->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();echo $instance->name . "\n";}
}

高级用法

指定属性的适用范围

PHP 提供了 Attribute::TARGET_* 来限定属性可以应用的位置。

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
class OnlyForClassAndMethod {}

这样,OnlyForClassAndMethod 只能用于类和方法,如果用于属性,则会报错。

应用同一个属性多次
#[MyAttribute("First"), MyAttribute("Second")]
class Example {}

注意:同一个属性应用了多次,则需要属性本身支持 Attribute::IS_REPEATABLE 可重复应用的目标

同时应用多个不同的属性

PHP 8 允许在同一元素(类、方法、属性等)上使用多个不同的属性,只需使用 多个 #[...] 语法 或 在 #[...] 内逗号分隔多个属性。

  • 多个 #[...] 语法
#[Attribute]
class FirstAttribute {}#[Attribute]
class SecondAttribute {}#[FirstAttribute]
#[SecondAttribute]
class MultiAttributeClass {}
  • 在同一个 #[...] 里使用逗号分隔
#[FirstAttribute, SecondAttribute]
class AnotherMultiAttributeClass {}
带参数的属性
#[Attribute]
class Route {public function __construct(public string $path, public string $method = "GET") {}
}#[Route("/home", "GET")]
class HomeController {}

解析:

$reflection = new ReflectionClass(HomeController::class);
$attributes = $reflection->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();echo "Path: " . $instance->path . ", Method: " . $instance->method;
}
同时使用带参数和不带参数的属性
#[Attribute]
class Role {public function __construct(public string $roleName) {}
}#[Attribute]
class Loggable {}#[Role("Admin"), Loggable]
class UserService {}

解析:

$reflection = new ReflectionClass(UserService::class);
$attributes = $reflection->getAttributes();foreach ($attributes as $attribute) {$instance = $attribute->newInstance();if ($instance instanceof Role) {echo "Role: " . $instance->roleName . PHP_EOL;} else {echo "Attribute: " . $attribute->getName() . PHP_EOL;}
}

输出:

Role: Admin
Attribute: Loggable

实际应用场景

路由映射(模拟 Laravel 路由)
#[Attribute]
class Route {public function __construct(public string $path, public string $method = "GET") {}
}class MyController {#[Route('/users', 'GET')]public function getUsers() {}#[Route('/users', 'POST')]public function createUser() {}
}// 解析控制器的方法路由
$reflection = new ReflectionClass(MyController::class);
foreach ($reflection->getMethods() as $method) {foreach ($method->getAttributes(Route::class) as $attribute) {$route = $attribute->newInstance();echo "Method: {$route->method}, Path: {$route->path}" . PHP_EOL;}
}

输出:

Method: GET, Path: /users
Method: POST, Path: /users

文章转载自:

http://luk35OE7.zLgbx.cn
http://c4snsn4F.zLgbx.cn
http://YJL7en3O.zLgbx.cn
http://X0NveTx8.zLgbx.cn
http://WuJJqMUz.zLgbx.cn
http://TTWYKDLE.zLgbx.cn
http://aYn7sUiL.zLgbx.cn
http://IhxV6UOT.zLgbx.cn
http://54HcQZB9.zLgbx.cn
http://FLzx7NvG.zLgbx.cn
http://4eEwGopt.zLgbx.cn
http://qrX3gpRs.zLgbx.cn
http://Fs1yGk9a.zLgbx.cn
http://U97yrNnP.zLgbx.cn
http://U3qtkLRG.zLgbx.cn
http://XlkBYo91.zLgbx.cn
http://XGytT4GJ.zLgbx.cn
http://RdrvJrvq.zLgbx.cn
http://2kCRTUzd.zLgbx.cn
http://vgcdNSnq.zLgbx.cn
http://sU1QKjv6.zLgbx.cn
http://xuMDEpij.zLgbx.cn
http://1IxNXusM.zLgbx.cn
http://ALsf6gHD.zLgbx.cn
http://B50Pb5QC.zLgbx.cn
http://D1KSlqZU.zLgbx.cn
http://Df8GECoF.zLgbx.cn
http://uRtvbJsU.zLgbx.cn
http://ZJeUja7A.zLgbx.cn
http://gOUILys3.zLgbx.cn
http://www.dtcms.com/wzjs/637822.html

相关文章:

  • 学生处网站建设招标公告做网站多少钱一个
  • 学校网站建设必要性怎么下载app到手机上
  • 局域网建设网站工具页面设计自述
  • 纯静态企业网站模板免费下载小程序商店网址
  • 查询网站备案公共服务网站系统建设方案
  • 上海营销型网站网站营销网站建设
  • 苏州制作网站的公司简介wordpress 版本 php7
  • 达州网站建设公司郑州专业建网站
  • 免费爱做网站商城网站建设公司地址
  • 遂宁门户网站建设先进工作单位wordpress菜单栏功能
  • 江西网站设计哪家强在线长链接转短链接
  • 网站设计赏析漫画驿站网页设计图纸尺寸图
  • 长春市网站建设网站设计模板免费建站
  • 北京响应式网站制作公司建设网站地图
  • 公司网站建设原则网站导航建设注意事项
  • wordpress html5 主题百度seo快速提升排名
  • php做的网站出现404wordpress widget
  • 网站仿静态百度成都总部
  • 英文网站建设 深圳太原网站建设网格未来
  • 成都哪家公司做网站比较好群晖套件做网站
  • 做网站到八方资源网怎么样网站制作公司浩森宇特
  • 做外贸做网站wordpress悬浮广告
  • 把手机做网站服务器网站开发属于软件开发
  • 推荐佛山顺德网站建设网站模板加后台
  • wap网站html5山东网站备案公司
  • 凡客建设网站稳定吗程序员培训学校
  • 网站建设华企云商快站淘客中转页
  • 中国搜索引擎网站排名广州市中智软件开发有限公司
  • 北京网站优化方法谢馥春网站建设的优势
  • 上海松江做网站的公司黄金网站软件app视频