PHP properties in hindi-php प्रॉपर्टीज क्या है?

हेल्लो दोस्तों आज के इस पोस्ट में आपको PHP properties in hindi  के बारे में बताया गया है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

Introduction to PHP Properties 

PHP में class member variables को properties कहा जाता है। हालाँकि इन्हें attributes और fields के नाम से भी define किया जाता है लेकिन standard form में इन्हें properties ही कहा जाता है।

एक property class से related data को store करती है। जिस प्रकार किसी छात्र के identity card पर उस छात्र से related information होती है। उसी प्रकार किसी class में create की गयी properties (variables) उस class से related information को store करती है।

Properties पर ही operations perform करने के लिए classes में methods define किये जाते है। उदाहरण के लिए आपने Student नाम की एक class create की है।

class Student
{
// Student properties here…
}

इस class में आप properties के द्वारा student से related कोई भी information store कर सकते है और उस पर operations perform कर सकते है। उदाहरण के लिए आप Student का नाम, पता, Course आदि properties में store कर सकते है और उन्हें webpage में display करवा सकते है।

इस प्रकार आपको जितने students के बारे में information store करने की आवश्यकता हो आप उतने ही class के object create करके उन students से related information properties के माध्यम से store कर सकते है।

PHP में static और non-static 2 प्रकार की properties होती है। इन दोनों में main difference यह है की static properties को class object के बजाय class के नाम द्वारा access और modify किया जाता है और non static properties को class के object द्वारा access किया जाता है।

PHP4 और PHP5 को एक दूसरे के लिए compatible बनाने के लिए var keyword का उपयोग अभी भी valid रखा गया है। यदि आप किसी property को var keyword द्वारा भी declare करते है तो वह भी valid मानी जायेगी। Run time में var keyword automatically public visibility mode द्वारा replace कर दिया जाता है।

Static Properties 

Static properties वे properties होती है जिनकी values सभी objects में समान होती है। जब आपको ऐसी property की आवश्यकता हो जिसकी value सभी objects में समान रहे और किसी object द्वारा modify ना की जा सके तो उसे आप static define कर सकते है।

उदाहरण के लिए आप चाहते है की सभी students की fees समान रहे और इसे modify ना किया जा सके तो आप Fees को static property के रूप में define कर सकते है। ऐसा करने से जितने भी Student class के object आप create करेंगे उन में Fees property की value समान रहेगी और उसे change भी नहीं किया जा सकेगा।

Syntax of Static Properties

Static properties declare करने का general syntax निचे दिया जा रहा है। 

[visibilityMode] [staticKeyword] [propertyName] = [constantValue];
  • visibilityMode – PHP में properties और methods का protection level define करने के लिए public, private और protected 3 visibility modes available है। इनके बारे में और अधिक आप PHP Visibility tutorial से जान सकते है। 
  • staticKeyword – Static properties को static keyword द्वारा define किया जाता है। 
  • propertyName – एक class में declare की गयी सभी properties का नाम unique होना चाहिए। 
  • constantValue – यदि आप property को initialize कर रहे है तो ऐसा एक constant value द्वारा किया जाना चाहिए। आपके द्वारा define की गयी value compile time में evaluate होने में सक्ष्म होनी चाहिए। यह value run time information पर आधारित नहीं होनी चाहिए। 

यदि आप कोई भी visibility mode define नहीं करते है तो by default static properties public define की जाती है।

Accessing Static Properties 

किसी भी static property को class के बाहर class के नाम और scope resolution (::) operator द्वारा access किया जाता है। इसका syntax निचे दिया जा रहा है।

[className] [scopeResolutionOp] [$] [propertyName];

इसी प्रकार आप चाहे तो static property को class के बाहर intialize भी कर सकते है।

[className] [scopeResolutionOp] [$] [propertyName] = [constantValue];

Example  

Static property का एक complete उदाहरण निचे दिया जा रहा है।

<?php   class myClass
{
// Creating static property
public static $name = “Best Hindi Tutorials”;
} // Accessing static property
echo “Name is : “.myClass::$name; ?>

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

Non-static Properties

Non static properties वे properties होती है जो अलग अलग class objects के लिए अलग अलग information store कर सकती है। उदाहरण के लिए आपको Student class में name property को non static define करना चाहिए क्योंकि सभी students के नाम अलग अलग होते है।

Non static properties को class objects द्वारा access और modify किया जा सकता है। यदि किसी student को अपने नाम में correction करना हो तो ऐसा किया जा सकता है। 

Syntax of Non-static Properties

PHP में non-static properties declare करने का general syntax निचे दिया जा रहा है। 

[visibilityMode] [propertyName];

Properties को declare करने के साथ साथ आप उन्हें initialize भी कर सकते है। इसका syntax निचे दिया जा रहा है। 

[visibilityMode] [propertyName] = [constantValue];

इन terms के बारे में static properties वाले section में बताया जा चूका है। 

Accessing Non-static Properties

Non-static properties class के object द्वारा access की जाती है। इसके लिए आप (->) object operator use करते है। इसका syntax निचे दिया जा रहा है।

[objectName] [objectOperator] [$] [propertyName];

इसी तरीके से non-static properties को initialize भी किया जा सकता है। इसका syntax निचे दिया जा रहा है।

[objectName] [objectOperator] [$] [propertyName] = [constantValue];

Example 

Non-static property का complete उदाहरण निचे दिया जा रहा है।

<?php   class myClass
{
// A non static property
public $name = “Best Hindi Tutorials”;
} $obj = new myClass(); // Accessing non static property
echo “Name is “.$obj->name; ?>

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है। 

reference-https://www.php.net/manual/en/language.oop5.properties.php

निवेदन-अगर आपको यह आर्टिकल(PHP properties in hindi) अच्छा लगा हो तो आप इस पोस्ट(PHP properties in hindi) को अपने दोस्तों के साथ जरुर शेयर करे और आपको जिस टॉपिक पर आपको पढना या नोट्स(PHP properties in hindi) चाहिए तो हमें जरुर कमेंट करे आपका कमेंट्स(PHP properties in hindi) हमारे लिए बहु मूल्य है धन्यवाद

Leave a Comment