PHP Class And Object In Hindi

📔 : PHP 🔗

जैसा कि आपने पिछले topic में पढ़ा कि, Class किसी Object के लिए एक Blueprint होता है। जैसे Real World Entity की कुछ Properties या Behavior होता है जिसे Programing में Class variables & methods से represent किया जाता है।


PHP में class define करने के लिए predefined keyword class का use किया जाता है , और उसके बाद class का name define किया जाता है।

PHP class Syntax

<?php  
class ClassName
{
/**
here you can define class members
members means variables or methods
*/
}
?>

कुछ इस तरह से PHP में class define की जाती है। इस class में हम class members variables / methods define करते हैं।

PHP Initiating Class Object

अब हम देखेंगे की कैसे class का Object बनाते हैं। PHP में predefined keyword new का use किसी class का Object बनाने के लिए किया जाता है।

$myObj = new ClassName();

That's it , इस तरह से class का Object initiate किया जाता है , यहाँ $myObj , class ClassName के Object को represent कर रहा है।

PHP class Example

अब हम एक example के through समझेंगे , और उस Object के type को predefined function var_dump() से check करेंगे।

File : php_class.php

Copy Fullscreen Close Fullscreen
<?php  
class ClassName
{
/** define variables or methods*/
}
$myObj = new ClassName();
/** now check the type of of this variable*/
echo var_dump($myObj);
?>
Output
object(ClassName)#1 (0) {} 

Explain :
Output में दिखाया गया है , कि print किया गया variable किस type का है , यह ठीक उसी तरह से है जैसे कोई normal variable का type check करते हैं।


अब चूंकि , जब हम variable int / float लेते थे तो उसका type int / float उसकी value के साथ print होता था , ठीक उसी तरह से Object का type वह class होती है , और curly braces {} में define की गयी सभी properties उनकी value के साथ show होती है।


हालाँकि अभी class में कोई property नहीं है , इसलिए कुछ भी show नहीं हो रहा है।

PHP Types Of Members In Class

किसी भी class में 2 tyoes के members (Variables / Methods) हो सकते हैं -

  1. Static members - इन्हे हम बिना Object initiate किये भी access कर सकते हैं। Read More ...
  2. Non Static members - इन्हे हम Object initiate किये बिना access नहीं कर सकते हैं। Read More...

By Default define किये गए methods और variables का type Non Static ही रहता है , हालाँकि इनके बारे में आप Next Chapter में details से पढ़ेंगे।

PHP Class Visibility

इसके अलावा Class में methods और variables define करने से पहले उनकी Visibility भी define करनी पड़ती है , यह 3 types की होती है -

  1. Private- इन्हे सिर्फ class के अंदर ही access किया जा सकता है।
  2. Protected - इन्हे class के अलावा इनकी Child Class में भी access किया जा सकता है।
  3. Public - और इन्हे कही से भी access किया जा सकता है।

Read More About Class Visibility

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers