PHP var_export() Function In Hindi | var_export() In PHP In Hindi
Insertion Sort in PHP With Example | How Does Insertion Sort Work in PHP
PHP Interview Questions In Hindi : PHP Interview Preparation
PHP filter_var() Function In Hindi | filter_var() In PHP In Hindi
what are callback functions in JavaScript
Optional chaining in JavaScript In Hindi | JS Optional chaining In Hindi
Generator Functions In JavaScript In Hindi | JavaScript Generator Functions
हैकिंग क्या है ? हैकिंग कितने प्रकार की होती है।
What is IP Address In Hindi | What is Internet Protocol , Types and It's Classes
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
PHP में stdClass
एक generic empty class है जिसमे dynamic properties & methods होती हैं , dynamic का मतलब है आप need के according properties को add या remove कर सकते हैं।
stdClass
के objects को new
operator या object typecast करके किया जा सकता है। कई सारे PHP functions जैसे json_decode(), mysqli_fetch_object() या PDOStatement::fetchObject() भी इसी class के instances को create करते हैं।
<?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
हालाँकि अगर आप चाहे तो 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
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
ध्यान रहे 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
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
के बारे में अच्छे से समझ आ गया होगा।
Loading ...
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