PHP stdClass In Hindi | stdClass In PHP

Blogs ❯❯ PHP

What is stdClass in PHP ?

PHP में stdClass एक generic empty class है जिसमे dynamic properties & methods होती हैं , dynamic का मतलब है आप need के according properties को add या remove कर सकते हैं।

Image could not load

PHP stdClass

stdClass के objects को new operator या object typecast करके किया जा सकता है। कई सारे PHP functions जैसे json_decode(), mysqli_fetch_object() या PDOStatement::fetchObject() भी इसी class के instances को create करते हैं।

PHP stdClass Example

<?php $obj = new stdClass; $obj->name = "Babu Rao"; $obj->age = 50; ?>

same Object instance का use करके आप सभी properties को access कर सकते हैं।

echo "Name : {$obj->name}"; \\ Babu Rao echo "Age : {$obj->age}"; \\ 50

PHP Add Method to stdClass

हालाँकि अगर आप चाहे तो methods को भी add कर सकते हैं लेकिन methods को call करने का तरीका थोड़ा different होता है।

क्योंकि हम method को एक value की तरह assign कर रहे हैं , तो उसी तरह से call भी करेंगे।

Example

<?php $obj = new stdClass; // assign method. $obj->speak = function() { echo "Utha le re BABA"; }; // now call the method. ($obj->speak)(); //or $speak = $obj->speak; $speak(); ?>

Output

Utha le re BABA
Utha le re BABA

define parameters

Next , आप need के according parameters भी define कर सकते हैं।

<?php $obj = new stdClass; // assign method. $obj->speak = function($talk) { echo $talk . "\n"; }; // pass value. ($obj->speak)("Kaho han !"); //or $speak = $obj->speak; $speak("150 rupaya dega"); ?>

Output

Kaho han !
150 rupaya dega

Access external variable in method

ध्यान रहे add की गए method के अंदर आप किसी external variable को directly access नहीं कर सकते हैं। external variable को access करने के लिए आपको use keyword का use करना पड़ेगा।

<?php $obj = new stdClass; $ex_var = "External variable"; $obj->speak = function() use ($ex_var) { echo $ex_var; }; ($obj->speak)(); ?>

Output

External variable

PHP delete properties from stdClass

stdClass Object से properties को delete करने के लिए unset() Function का use किया जाता है।

<?php $obj = new stdClass; $obj->name = "Babu Rao"; $obj->age = 50; echo "Before : "; var_dump($obj); // remove age. unset($obj->age); echo "After : "; var_dump($obj); ?>

Output

Before : object(stdClass)#1 (2) {
  ["name"]=>
  string(8) "Babu Rao"
  ["age"]=>
  int(50)
}
After : object(stdClass)#1 (1) {
  ["name"]=>
  string(8) "Babu Rao"
}

var_dump() function किसी variable के structure / information जैसे stored value उसका type , length etc. के बारे में बताता है।

Read more about var_dump() ...

I Hope, आपको PHP में stdClass के बारे में अच्छे से समझ आ गया होगा।

Recent Blogs

Loading ...

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