PHP Security Tips : Hiding php In URL (In Hindi)
Insertion Sort in PHP With Example | How Does Insertion Sort Work in PHP
Selection Sort in PHP With Example | How does selection sort work in PHP ?
PHP stdClass In Hindi | stdClass In PHP
PHP Interview Questions In Hindi : PHP Interview Preparation
What is DNS In Hindi ? DNS kya hai?
Java Program to check whether number is Palindrome or not | Java Find Palindrome Number
Generator Functions In JavaScript In Hindi | JavaScript Generator Functions
Variables In Snowflake In Hindi | Snowflake Variables In Hindi
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.
बैसे तो आपने normal data types जैसे int , string , boolean etc को एक दुसरे में type cast किया होगा , लेकिन आज हम इस article में PHP में object type casting के बारे में बात करेंगे।
PHP में किसी दूसरे data type को object बनाने के लिए object
keyword का ही use किया जाता है।
For Example
$x = 10;
$obj = (object) $x;
अब जैसा कि आपको पता है कि किसी भी Object में properties होती हैं लेकिन primitive values (like : int, bool, string etc.) तो normal value हैं इनमे कोई property तो है नहीं , तो जब हम primitive values को object में type cast करते हैं तो object में एक default property scalar
होती है।
यही scalar
property हमारी actual primitive value को contain करती है।
<?php
$x = "Hello Object !";
$obj = (object)$x;
var_dump($obj);
// you can access the actual value using scalar property.
echo $obj->scalar;
?>
Output
object(stdClass)#1 (1) { ["scalar"]=> string(14) "Hello Object !" } Hello Object !
हालाँकि अगर आप किसी Array को object में convert करेंगे तो आपको properties ही मिलेंगी जिनकी values को आप array key के according access कर सकते हैं।
<?php
$x = [
"name" => "Babu Rao",
"age" => "60",
];
$obj = (object) $x;
var_dump($obj);
// you can access the actual value using array key name.
echo "Name : $obj->name \n";
echo "Age : $obj->age";
?>
Output
object(stdClass)#1 (2) { ["name"]=> string(8) "Babu Rao" ["age"]=> string(2) "60" } Name : Babu Rao Age : 60
ऊपर दिया गया example तो Associative Array के लिए था , Indexed Array के case में सभी index , property की तरह work करेंगी।
Example
<?php
$persons = ["Raju", "Shyam", "Babu Rao"];
$obj = (object) $persons;
var_dump($obj);
// you can access the actual value using array key name.
echo "Person 1 : ". $obj->{"0"};
echo "Person 2 : ". $obj->{"1"};
echo "Person 3 : ". $obj->{"2"};
?>
object(stdClass)#1 (3) { ["0"]=> string(4) "Raju" ["1"]=> string(5) "Shyam" ["2"]=> string(8) "Babu Rao" } Person 1 : Raju Person 2 : Shyam Person 3 : Babu Rao
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