PHP Enumerations : Enums In PHP In Hindi

Other Blogs

Blogs ❯❯ PHP

Image could not load

PHP Enumeration

PHP 8.1 में एक feature introduce किया गया है — Enumerations (Enums). अगर आपको अपने code में fixed set of values को handle करना है, तो Enums आपके लिए game-changer हो सकते हैं।

Enums क्या होते हैं?

Enum एक special class होती है जिसमे predefined constant values define कि जाती हैं। यह आपको type safety और code clarity provide करते हैं।

PHP में Enums को enum keyword से declare किया जाता है।

PHP Enum Example

enum UserRole {  
    case ADMIN;  
    case EDITOR;  
    case VIEWER;  
}  

यहां UserRole एक Enum है जिसमे 3 cases हैं -​

Use case

function checkAccess(UserRole $role) { if ($role === UserRole::ADMIN) { echo "Full Access!"; } } checkAccess(UserRole::ADMIN); //Works checkAccess("ADMIN"); // Error: Only UserRole allowed!

Backed Enums

अगर आपको हर case के साथ एक specific value assign करनी है, तो आप Backed Enums का use कर सकते हैं।

enum UserRole: int { case ADMIN = 1; case EDITOR = 2; case VIEWER = 3; }

यहां हर case को एक string value assign कि गयी है।

Accessing Enum Values

Enum में हर case अपने आप में Enum type का एक object होता है इसलिए आपको उस particular case के साथ values access करनी पड़ेगी।

echo UserRole::ADMIN->value; // Output: 1

हम value से भी Enum case get कर सकते है।

$roleId = 2; $role = UserRole::from($roleId); // EDITOR return karega // Safe version $role = UserRole::tryFrom(5); // Agar invalid ho, toh null dega

Methods In Enums

Enums में आप methods भी define कर सकते हैं -

enum HttpStatus: int { case OK = 200; case NOT_FOUND = 404; public function isError(): bool { return $this->value >= 400; } } HttpStatus::NOT_FOUND->isError(); // true

Traits In Enum

PHP Enums में Traits का use किया जा सकता है, लेकिन एक important बात याद रखें : Traits में properties नहीं होनी चाहिए।

अगर Trait में properties हैं, तो PHP fatal error देगा, Traits सिर्फ methods और static methods contain कर सकते हैं।

trait EnumToArray { public static function names(): array { return array_column(self::cases(), 'name'); } public static function values(): array { return array_column(self::cases(), 'value'); } public static function asArray(): array { if (empty(self::values())) { return self::names(); } if (empty(self::names())) { return self::values(); } return array_column(self::cases(), 'value', 'name'); } }

इस Trait को आप अपने Enums में use कर सकते हैं, जिससे आपको cases के names, values, या दोनो as array मिल जायेंगे।

enum UserRole: int { case ADMIN = 1; case EDITOR = 2; case VIEWER = 3; // use trait use EnumToArray; } print_r(UserRole::names()); // Array ( [0] => ADMIN [1] => EDITOR [2] => VIEWER ) print_r(UserRole::values()); // Array ( [0] => 1 [1] => 2 [2] => 3 ) print_r(UserRole::asArray()); // Array ( [ADMIN] => 1 [EDITOR] => 2 [VIEWER] => 3 )

Implement Interface In Enum

आप Interface को Enum में implement भी कर सकते हैं, जिससे code reuse और contract enforcement का आपको advantage मिलेगा।

interface Describable { public function description(): string; } enum ServerStatus: string implements Describable { case PENDING = 'pending'; case RUNNING = 'running'; case STOPPED = 'stopped'; case FAILED = 'failed'; public function description(): string { return match ($this) { self::PENDING => 'The server is currently being created or initialized.', self::RUNNING => 'The server is live and operational.', self::STOPPED => 'The server is stopped but can be restarted.', self::FAILED => 'The server encountered an error and is not running.', }; } } echo ServerStatus::RUNNING->description(); // Output : The server is live and operational.

Keynote ⚠️

  • Traits में properties नहीं होनी चाहिए, अगर properties हैं, तो PHP fatal error देगा।

  • Backend Enum case में assign की जाने वाली value duplicate नहीं होनी चाहिए।

  • Enums को extend नहीं किया जा सकता , और न ही किसी class को extend कर सकते हैं । क्योंकि एक class में properties होती है। 

  • Interfaces को implement करने से पहले, ensure करें कि enum के सारे required methods defined हो। 

  • Enums का object (new Enum()) नहीं बना सकते हैं।

Conclusion

PHP Enums में Traits और Interfaces का use करके आप अपने code को ज़्यादा modular, reusable, और maintainable बना सकते हैं।

यह approach especially तब useful है जब आपको multiple enums में common functionality चाहिए हो। Traits से आप code reuse कर सकते हैं और Interfaces से आप contract enforce कर सकते हैं।

अगर आपको ये blog पसंद आया है और अपने कुछ नया सीखा तो अपने friends के साथ share करना न भूले !

Happy coding :)

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !