{"id":28376,"date":"2024-11-24T16:14:39","date_gmt":"2024-11-24T08:14:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28376\/"},"modified":"2024-11-24T16:14:39","modified_gmt":"2024-11-24T08:14:39","slug":"working-with-php-attributes-dos-donts","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28376\/","title":{"rendered":"Working with PHP Attributes: Do\u2019s &#038; Don\u2019ts"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173158336446706.jpg\" class=\"aligncenter\" title=\"Working with PHP Attributes: Do\u2019s &#038; Don\u2019ts\u63d2\u56fe\" alt=\"Working with PHP Attributes: Do\u2019s &#038; Don\u2019ts\u63d2\u56fe\" \/><\/p>\n<p>php \u4e2d\u7684\u5c5e\u6027\u5141\u8bb8\u60a8\u76f4\u63a5\u4f7f\u7528\u5143\u6570\u636e\u6ce8\u91ca\u4ee3\u7801\u5143\u7d20\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u4ee3\u7801\u914d\u7f6e\uff0c\u4ece\u800c\u53ef\u80fd\u51cf\u5c11  \u7b49\u6846\u67b6\u4e2d\u7684\u6837\u677f\u6587\u4ef6\u3002\u7136\u800c\uff0c\u4e0e\u4efb\u4f55\u529f\u80fd\u4e00\u6837\uff0c\u5c5e\u6027\u53ef\u80fd\u4f1a\u88ab\u8fc7\u5ea6\u4f7f\u7528\u6216\u8bef\u7528\uff0c\u4ece\u800c\u5bfc\u81f4\u63a7\u5236\u5668\u6df7\u4e71\u548c\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u4ee5\u589e\u5f3a\u4ee3\u7801\u6e05\u6670\u5ea6\u7684\u65b9\u5f0f\u4f7f\u7528\u5c5e\u6027\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6211\u8fd8\u5c06\u63d0\u4f9b\u4e00\u4e2a\u201c\u8be5\u505a\u548c\u4e0d\u8be5\u505a\u201d\u7684\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u6b21\u6bd4\u8f83\u7684\u793a\u4f8b\uff0c\u7a81\u51fa\u663e\u793a\u5c5e\u6027\u5de5\u4f5c\u826f\u597d\u7684\u573a\u666f\u4ee5\u53ca\u53ef\u80fd\u4e0d\u5de5\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<h3> 1. \u7406\u89e3php\u4e2d\u7684\u5c5e\u6027 <\/h3>\n<p>\u8fd9\u662f\u5b9a\u4e49\u548c\u4f7f\u7528\u5c5e\u6027\u6765\u63d0\u4f9b\u4e00\u4e9b\u4e0a\u4e0b\u6587\u7684\u5feb\u901f\u793a\u4f8b\uff1a<\/p>\n<pre>#[attribute]\nclass mycustomattribute {\n    public function __construct(public string $description) {}\n}\n\n#[mycustomattribute(\"this is a test class\")]\nclass myclass {\n    #[mycustomattribute(\"this is a test method\")]\n    public function mymethod() {}\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 2. \u6ce8\u610f\u4e8b\u9879\uff1a\u5feb\u901f\u6982\u8ff0 <\/h3>\n<p>\u4e0b\u8868\u603b\u7ed3\u4e86\u6700\u4f73\u5b9e\u8df5\u548c\u5e38\u89c1\u9677\u9631\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>do\u2019s<\/strong><\/th>\n<th><strong>don\u2019ts<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>use attributes for standard, repetitive configurations (e.g., http methods, caching).<\/td>\n<td>don\u2019t overload attributes with complex configurations or conditional logic.<\/td>\n<\/tr>\n<tr>\n<td>leverage attributes for metadata rather than core application logic.<\/td>\n<td>avoid embedding business logic or intricate rules within attributes.<\/td>\n<\/tr>\n<tr>\n<td>apply attributes for simple, reusable annotations (e.g., #[throttle], #[cache]).<\/td>\n<td>don\u2019t try to replace laravel\u2019s route files entirely with attribute-based routing.<\/td>\n<\/tr>\n<tr>\n<td>cache attribute-based reflections when possible to improve performance.<\/td>\n<td>don\u2019t rely solely on attributes for configurations that need flexibility or change often.<\/td>\n<\/tr>\n<tr>\n<td>document your attributes, so team members understand their purpose and usage.<\/td>\n<td>avoid using attributes for configurations where traditional methods work better (e.g., middleware settings).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3> 3. \u5b9e\u4f8b\u8be6\u7ec6\u6bd4\u8f83 <\/h3>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u5177\u4f53\u793a\u4f8b\u6df1\u5165\u4e86\u89e3\u6bcf\u4e2a\u6bd4\u8f83\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<h4> 1. \u4f7f\u7528\u5c5e\u6027\u8fdb\u884c\u6807\u51c6\u3001\u91cd\u590d\u7684\u914d\u7f6e\uff08\u6267\u884c\uff09 <\/h4>\n<p>\u5c5e\u6027\u975e\u5e38\u9002\u5408\u4e0d\u9700\u8981\u590d\u6742\u903b\u8f91\u7684\u6807\u51c6\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e09\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li> <strong>\u5b9a\u4e49\u8def\u7531<\/strong>\uff1a\u4f7f\u7528\u5c5e\u6027\u901a\u8fc7 http \u65b9\u6cd5\u548c\u8def\u5f84\u5b9a\u4e49\u7b80\u5355\u7684\u8def\u7531\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class route {\n      public function __construct(public string $method, public string $path) {}\n  }\n\n  class productcontroller {\n      #[route('get', '\/products')]\n      public function index() {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u7f13\u5b58\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528\u5c5e\u6027\u6307\u5b9a\u65b9\u6cd5\u7684\u7f13\u5b58\u6301\u7eed\u65f6\u95f4\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class cache {\n      public function __construct(public int $duration) {}\n  }\n\n  class productcontroller {\n      #[cache(3600)]\n      public function show($id) {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u901f\u7387\u9650\u5236<\/strong>\uff1athrottle \u5c5e\u6027\u53ef\u7528\u4e8e\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u8bf7\u6c42\u6570\u91cf\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class throttle {\n      public function __construct(public int $maxattempts) {}\n  }\n\n  class usercontroller {\n      #[throttle(5)]\n      public function store() {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> \u4e0d\u8981\u4f7f\u7528\u590d\u6742\u7684\u914d\u7f6e\u6765\u91cd\u8f7d\u5c5e\u6027\uff08\u4e0d\u8981\uff09 <\/h4>\n<p>\u907f\u514d\u5bf9\u9700\u8981\u591a\u4e2a\u53c2\u6570\u6216\u6761\u4ef6\u7684\u914d\u7f6e\u4f7f\u7528\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e0d\u5e94\u8be5\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li> <strong>\u4f7f\u7528\u591a\u4e2a\u914d\u7f6e\u91cd\u8f7d<\/strong>\uff1a\u907f\u514d\u5411\u5c5e\u6027\u6dfb\u52a0\u591a\u4e2a\u53c2\u6570\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class route {\n      public function __construct(\n          public string $method,\n          public string $path,\n          public ?string $middleware = null,\n          public ?string $prefix = null\n      ) {}\n  }\n\n  #[route('post', '\/users', middleware: 'auth', prefix: '\/admin')]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u5c5e\u6027\u4e2d\u7684\u6761\u4ef6\u903b\u8f91<\/strong>\uff1a\u907f\u514d\u5c5e\u6027\u5185\u7684\u6761\u4ef6\u8bbe\u7f6e\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class condition {\n      public function __construct(public string $condition) {}\n  }\n\n  class controller {\n      #[condition(\"isadmin() ? 'adminroute' : 'userroute'\")]\n      public function index() {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u5355\u4e2a\u5c5e\u6027\u4e2d\u7684\u94fe\u63a5\u914d\u7f6e<\/strong>\uff1a\u907f\u514d\u5728\u4e00\u4e2a\u5c5e\u6027\u4e2d\u94fe\u63a5\u591a\u4e2a\u914d\u7f6e\u884c\u4e3a\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class combined {\n      public function __construct(\n          public int $cacheduration,\n          public int $ratelimit\n      ) {}\n  }\n\n  #[combined(cacheduration: 300, ratelimit: 5)]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> 2. \u5229\u7528\u5143\u6570\u636e\u7684\u5c5e\u6027\uff08\u6267\u884c\uff09 <\/h4>\n<p>\u4f7f\u7528\u5c5e\u6027\u4f5c\u4e3a\u6807\u8bb0\u6216\u5143\u6570\u636e\uff0c\u800c\u4e0d\u662f\u5728\u5176\u4e2d\u5d4c\u5165\u5e94\u7528\u7a0b\u5e8f\u903b\u8f91\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li> <strong>\u9a8c\u8bc1\u6ce8\u91ca<\/strong>\uff1a\u4f7f\u7528\u5c5e\u6027\u5c06\u5b57\u6bb5\u6807\u8bb0\u4e3a\u5fc5\u586b\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class required {}\n\n  class user {\n      #[required]\n      public string $name;\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u6307\u5b9a http \u65b9\u6cd5\u4f5c\u4e3a\u5143\u6570\u636e<\/strong>\uff1a\u4f7f\u7528\u5c5e\u6027\u6765\u6807\u8bb0 http \u65b9\u6cd5\u7c7b\u578b\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class get {}\n\n  class blogcontroller {\n      #[get]\n      public function list() {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u6307\u793a\u8bbf\u95ee\u7ea7\u522b<\/strong>\uff1a\u4f7f\u7528\u5c5e\u6027\u6765\u6307\u793a\u8bbf\u95ee\u7ea7\u522b\u8981\u6c42\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class requiresadmin {}\n\n  class settingscontroller {\n      #[requiresadmin]\n      public function update() {}\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> \u4e0d\u8981\u5728\u5c5e\u6027\u4e2d\u5d4c\u5165\u4e1a\u52a1\u903b\u8f91\uff08\u4e0d\u8981\uff09 <\/h4>\n<p>\u907f\u514d\u4f7f\u7528\u5c5e\u6027\u76f4\u63a5\u786e\u5b9a\u5e94\u7528\u7a0b\u5e8f\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e0d\u5e94\u8be5\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li> <strong>\u907f\u514d\u5728\u5c5e\u6027\u4e2d\u4f7f\u7528\u76f4\u63a5\u6761\u4ef6<\/strong>\uff1a\u4e0d\u8981\u5728\u5c5e\u6027\u4e2d\u653e\u7f6e\u6761\u4ef6\u68c0\u67e5\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class accesscontrol {\n      public function __construct(public string $role) {}\n  }\n\n  #[accesscontrol(role: isadmin() ? 'admin' : 'user')]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u907f\u514d\u5728\u5c5e\u6027\u4e2d\u8c03\u7528\u65b9\u6cd5<\/strong>\uff1a\u4e0d\u8981\u5728\u5c5e\u6027\u4e2d\u653e\u7f6e\u51fd\u6570\u8c03\u7528\u6216\u4e1a\u52a1\u903b\u8f91\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class conditionalcache {\n      public function __construct(public int $duration) {}\n  }\n\n  #[conditionalcache(duration: userhaspremium() ? 3600 : 300)]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u907f\u514d\u5728\u5c5e\u6027\u4e2d\u4f7f\u7528\u8ba1\u7b97\u503c<\/strong>\uff1a\u5c5e\u6027\u5e94\u8be5\u662f\u9759\u6001\u5143\u6570\u636e\uff0c\u800c\u4e0d\u662f\u8ba1\u7b97\u503c\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class cache {\n      public function __construct(public int $duration) {}\n  }\n\n  #[cache(duration: (int)env('cache_duration'))]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> 3. \u5c06\u5c5e\u6027\u5e94\u7528\u4e8e\u7b80\u5355\u3001\u53ef\u91cd\u7528\u7684\u6ce8\u91ca\uff08\u6267\u884c\uff09 <\/h4>\n<p>\u5c5e\u6027\u975e\u5e38\u9002\u5408\u53ef\u91cd\u7528\u7684\u8f7b\u91cf\u7ea7\u6ce8\u91ca\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u91cd\u7528\u7684\u6ce8\u91ca\u793a\u4f8b\uff1a<\/p>\n<ul>\n<li> <strong>simple throttle<\/strong>\uff1a\u7528\u4e8e\u9650\u5236\u8bf7\u6c42\u901f\u7387\u7684\u7b80\u5355\u8282\u6d41\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class throttle {\n      public function __construct(public int $limit) {}\n  }\n\n  #[throttle(5)]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u7f13\u5b58\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528\u5355\u4e2a\u6301\u7eed\u65f6\u95f4\u53c2\u6570\u6dfb\u52a0\u7f13\u5b58\u63a7\u5236\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class cache {\n      public function __construct(public int $duration) {}\n  }\n\n  #[cache(120)]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u5f03\u7528\u8b66\u544a<\/strong>\uff1a\u5c06\u65b9\u6cd5\u6807\u8bb0\u4e3a\u5f03\u7528\u4ee5\u63d0\u9192\u5f00\u53d1\u4eba\u5458\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class deprecated {\n      public function __construct(public string $message) {}\n  }\n\n  #[deprecated(\"this method will be removed in v2.0\")]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> \u4e0d\u8981\u8fc7\u5ea6\u4f7f\u7528\u5176\u4ed6\u683c\u5f0f\u66f4\u5bb9\u6613\u7684\u914d\u7f6e\u5c5e\u6027\uff08\u4e0d\u8981\uff09 <\/h4>\n<p>\u67d0\u4e9b\u914d\u7f6e\u5728\u5c5e\u6027\u4e4b\u5916\u53ef\u4ee5\u5f97\u5230\u66f4\u597d\u7684\u7ba1\u7406\u3002\u4ee5\u4e0b\u662f\u4e0d\u5e94\u8be5\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li> <strong>\u4e2d\u95f4\u4ef6\u914d\u7f6e<\/strong>\uff1a\u907f\u514d\u76f4\u63a5\u5728\u5c5e\u6027\u4e2d\u914d\u7f6e\u4e2d\u95f4\u4ef6\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class middleware {\n      public function __construct(public string $name) {}\n  }\n\n  #[middleware('auth')]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u6388\u6743\u89c4\u5219<\/strong>\uff1a\u590d\u6742\u7684\u6388\u6743\u914d\u7f6e\u6700\u597d\u653e\u5728\u7b56\u7565\u6587\u4ef6\u4e2d\u3002 <\/li>\n<\/ul>\n<pre>  #[attribute]\n  class permission {\n      public function __construct(public string $requiredpermission) {}\n  }\n\n  #[permission(\"edit_post\")]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u590d\u6742\u7684\u9a8c\u8bc1\u89c4\u5219<\/strong>\uff1a\u5c06\u9a8c\u8bc1\u903b\u8f91\u6392\u9664\u5728\u5c5e\u6027\u4e4b\u5916\u3002 <\/li>\n<\/ul>\n<pre>  #[Attribute]\n  class Validate {\n      public function __construct(public array $rules) {}\n  }\n\n  #[Validate(['name' =&gt; 'required|min:3'])]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u7ed3\u8bba <\/h3>\n<p>\u5c5e\u6027\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u91cd\u590d\u914d\u7f6e\uff0c\u7279\u522b\u662f\u5728 laravel \u8fd9\u6837\u7684 php \u6846\u67b6\u4e2d\u3002 <\/p>\n<p>\u4f46\u662f\uff0c\u5b83\u4eec\u4f5c\u4e3a\u7b80\u5355\u7684\u5143\u6570\u636e\u6548\u679c\u6700\u597d\uff0c\u5e76\u4e14\u5fc5\u987b\u907f\u514d\u4f7f\u7528\u590d\u6742\u7684\u914d\u7f6e\u6216\u903b\u8f91\u4f7f\u5b83\u4eec\u8fc7\u8f7d\u3002 <\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u5e76\u5c06\u5c5e\u6027\u7528\u4f5c\u8f7b\u91cf\u7ea7\u3001\u53ef\u91cd\u7528\u7684\u6ce8\u91ca\uff0c\u60a8\u53ef\u4ee5\u5145\u5206\u5229\u7528\u5b83\u4eec\u7684\u6f5c\u529b\uff0c\u800c\u4e0d\u4f1a\u7ed9\u4ee3\u7801\u5e93\u589e\u52a0\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002<\/p>\n<hr>\n<h3> \u8d5e\u52a9 <\/h3>\n<p>\u901a\u8fc7\u5728 hub \u8d5e\u52a9\u5546\u4e0a\u8d5e\u52a9\u6211\u6765\u652f\u6301\u6211\u7684\u5f00\u6e90\u5de5\u4f5c\uff01\u60a8\u7684\u8d5e\u52a9\u5e2e\u52a9\u6211\u4e0d\u65ad\u521b\u5efa\u6709\u7528\u7684 laravel \u8f6f\u4ef6\u5305\u3001\u5de5\u5177\u548c\u6559\u80b2\u5185\u5bb9\uff0c\u8ba9\u5f00\u53d1\u8005\u793e\u533a\u53d7\u76ca\u3002\u611f\u8c22\u60a8\u5e2e\u52a9\u8ba9\u5f00\u6e90\u53d8\u5f97\u66f4\u597d\uff01<\/p>\n<hr>\n<p>\u7167\u7247\u7531 milad fakurian \u5728 unsplash \u4e0a\u62cd\u6444<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWorking with PHP Attributes: Do\u2019s &amp; Don\u2019ts\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php \u4e2d\u7684\u5c5e\u6027\u5141\u8bb8\u60a8\u76f4\u63a5\u4f7f\u7528\u5143\u6570\u636e\u6ce8\u91ca\u4ee3\u7801\u5143\u7d20\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u4ee3\u7801\u914d\u7f6e\uff0c\u4ece\u800c\u53ef\u80fd\u51cf\u5c11 \u7b49\u6846\u67b6\u4e2d\u7684\u6837\u677f\u6587\u4ef6\u3002\u7136\u800c\uff0c\u4e0e\u4efb\u4f55\u529f\u80fd\u4e00\u6837\uff0c\u5c5e\u6027\u53ef\u80fd\u4f1a\u88ab\u8fc7\u5ea6\u4f7f\u7528\u6216\u8bef\u7528\uff0c\u4ece\u800c\u5bfc\u81f4\u63a7\u5236\u5668\u6df7\u4e71\u548c\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u4ee5\u589e\u5f3a\u4ee3\u7801\u6e05\u6670\u5ea6\u7684\u65b9\u5f0f\u4f7f\u7528\u5c5e\u6027\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u6211\u8fd8\u5c06\u63d0\u4f9b\u4e00\u4e2a\u201c\u8be5\u505a\u548c\u4e0d\u8be5\u505a\u201d\u7684\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u6b21\u6bd4\u8f83\u7684\u793a\u4f8b\uff0c\u7a81\u51fa\u663e\u793a\u5c5e\u6027\u5de5\u4f5c\u826f\u597d\u7684\u573a\u666f\u4ee5\u53ca\u53ef\u80fd\u4e0d\u5de5\u4f5c\u7684\u573a\u666f\u3002 1. \u7406\u89e3php\u4e2d\u7684\u5c5e\u6027 \u8fd9\u662f\u5b9a\u4e49\u548c\u4f7f\u7528\u5c5e\u6027\u6765\u63d0\u4f9b\u4e00\u4e9b\u4e0a\u4e0b\u6587\u7684\u5feb\u901f\u793a\u4f8b\uff1a #[attribute] class mycustomattribute { public function __construct(public string $description) {} } #[mycustomattribute(&#8220;this is a test class&#8221;)] class myclass { #[mycustomattribute(&#8220;this is a test method&#8221;)] public function mymethod() {} } \u767b\u5f55\u540e\u590d\u5236 2. \u6ce8\u610f\u4e8b\u9879\uff1a\u5feb\u901f\u6982\u8ff0 \u4e0b\u8868\u603b\u7ed3\u4e86\u6700\u4f73\u5b9e\u8df5\u548c\u5e38\u89c1\u9677\u9631\uff1a do\u2019s don\u2019ts use attributes for standard, repetitive configurations (e.g., http methods, caching). don\u2019t overload attributes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-28376","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28376"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28376\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}