← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 17 of 24

PHP Attributes

Attributes sticky labels जैसे हैं जिन्हें आप किसी class या function से attach करके उसके बारे में extra information देते हैं। दूसरा code इन labels को पढ़ सकता है और इन पर act कर सकता है।
Syntax
php
#[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
<?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";
?>

Custom Attributes Define करना

एक custom attribute define करना बस एक normal class define करने और इसे built-in #[Attribute] annotation से mark करने जैसा है, जो PHP को बताता है कि यह class metadata की तरह इस्तेमाल होने के लिए है, usual तरीके से directly instantiate होने के लिए नहीं।

उदाहरण: Defining custom Attributes

php
<?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";
?>

Attributes Apply करना

एक attribute apply करने का मतलब है जिस class, method, या property को यह describe करता है उसके ठीक ऊपर सीधे #[YourAttribute] लिखना -- syntax compact है और उस code के साथ ही रहता है जिसे यह annotate करता है।

उदाहरण: Applying Attributes

php
<?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";
?>

Reflection से Attributes पढ़ना

Attributes पढ़े जाने तक inert रहते हैं: Reflection API का getAttributes() method उन्हें runtime पर retrieve करता है, जो है कि frameworks route registration या validation rules जैसे behavior drive करने के लिए attributes कैसे इस्तेमाल करते हैं।

उदाहरण: Reading Attributes with Reflection

php
<?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;
?>

Attributes बनाम PHPDoc

क्योंकि attributes एक अलग documentation-comment parser के बजाय खुद PHP engine द्वारा parse और validate किए जाते हैं, वे PHPDoc annotations से पढ़ने में तेज़ और silent typos के कहीं कम prone दोनों हैं।

उदाहरण: Attributes vs. PHPDoc

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि एक attribute अपने आप कुछ करेगा, जबकि यह सिर्फ metadata store करता है जिसे code को Reflection से पढ़ना ज़रूरी है।
  2. PHP 8.0 से पुराने version पर attributes इस्तेमाल करना, जहाँ #[...] को comment माना जाता है।
  3. किसी custom attribute class पर #[Attribute] marker भूल जाना, ताकि इसे attribute की तरह इस्तेमाल न किया जा सके।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.