PHP Attributes
In this page:
#[Attribute]
class AttributeName {
public function __construct(public type $property) {}
}
#[AttributeName(value)]
class ClassName { }
Attributes क्या हैं?
PHP 8.0 में introduce किए गए Attributes आपको native PHP syntax में structured metadata सीधे classes, methods, और properties से attach करने देते हैं, plain text की तरह PHPDoc comments parse करने की पुरानी ज़रूरत की जगह लेते हुए।
उदाहरण: What are Attributes?
<?php
#[Attribute]
// Define the class `Route`
class Route {
// Define the constructor `__construct` taking `public string $path`
public function __construct(public string $path) {}
}
#[Route('/home')]
class HomeController {}
// Print "Attributes attach structured metadata directly in PHP syntax" to the output
echo "Attributes attach structured metadata directly in PHP syntax";
?>
Login to try C/C++/Java/PHP code in the editor
Custom Attributes Define करना
एक custom attribute define करना बस एक normal class define करने और इसे built-in #[Attribute] annotation से mark करने जैसा है, जो PHP को बताता है कि यह class metadata की तरह इस्तेमाल होने के लिए है, usual तरीके से directly instantiate होने के लिए नहीं।
उदाहरण: Defining custom Attributes
<?php
#[Attribute]
// Define the class `Deprecated`
class Deprecated {
// Define the constructor `__construct` taking `public string $reason`
public function __construct(public string $reason = '') {}
}
// Print "Deprecated is now usable as an attribute" to the output
echo "Deprecated is now usable as an attribute";
?>
Login to try C/C++/Java/PHP code in the editor
Attributes Apply करना
एक attribute apply करने का मतलब है जिस class, method, या property को यह describe करता है उसके ठीक ऊपर सीधे #[YourAttribute] लिखना -- syntax compact है और उस code के साथ ही रहता है जिसे यह annotate करता है।
उदाहरण: Applying Attributes
<?php
#[Attribute]
// Define the class `Deprecated`
class Deprecated {
// Define the constructor `__construct` taking `public string $reason`
public function __construct(public string $reason = '') {}
}
// Define the class `Report`
class Report {
#[Deprecated('Use generate() instead')]
// Define the function `oldGenerate` with no parameters
function oldGenerate() {}
}
// Print "Attribute applied directly above the method" to the output
echo "Attribute applied directly above the method";
?>
Login to try C/C++/Java/PHP code in the editor
Reflection से Attributes पढ़ना
Attributes पढ़े जाने तक inert रहते हैं: Reflection API का getAttributes() method उन्हें runtime पर retrieve करता है, जो है कि frameworks route registration या validation rules जैसे behavior drive करने के लिए attributes कैसे इस्तेमाल करते हैं।
उदाहरण: Reading Attributes with Reflection
<?php
#[Attribute]
// Define the class `Route`
class Route {
// Define the constructor `__construct` taking `public string $path`
public function __construct(public string $path) {}
}
#[Route('/home')]
class HomeController {}
// Create a new `ReflectionClass` instance with `HomeController::class`, stored in `$reflection`
$reflection = new ReflectionClass(HomeController::class);
// Declare `$attributes`, set to `$reflection->getAttributes(Route::class)`
$attributes = $reflection->getAttributes(Route::class);
// Print `$attributes[0]->newInstance()->path` to the output
echo $attributes[0]->newInstance()->path;
?>
Login to try C/C++/Java/PHP code in the editor
Attributes बनाम PHPDoc
क्योंकि attributes एक अलग documentation-comment parser के बजाय खुद PHP engine द्वारा parse और validate किए जाते हैं, वे PHPDoc annotations से पढ़ने में तेज़ और silent typos के कहीं कम prone दोनों हैं।
उदाहरण: Attributes vs. PHPDoc
<?php
// PHPDoc (parsed as a comment, not validated by PHP):
// /** @route /home */
// Attribute (parsed and validated by the engine itself):
#[Attribute]
class Route {
public function __construct(public string $path) {}
}
echo "Attributes are engine-native, PHPDoc is just a comment convention";
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक attribute अपने आप कुछ करेगा, जबकि यह सिर्फ metadata store करता है जिसे code को Reflection से पढ़ना ज़रूरी है।
- PHP 8.0 से पुराने version पर attributes इस्तेमाल करना, जहाँ
#[...]को comment माना जाता है। - किसी custom attribute class पर
#[Attribute]marker भूल जाना, ताकि इसे attribute की तरह इस्तेमाल न किया जा सके।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser