PHP stdClass: Storing Data in an Object Instead of an Array

Have you ever seen a scenario where you’re accessing data as an object, such as when you’re using WordPress’s database interface or when you’re parsing XML files with SimpleXML? You have something like echo $result->name.

Have you ever wondered how that was done? Using PHP’s stdClass feature you can create an object, and assign data to it, without having to formally define a class.

Suppose you wanted to quickly create a new object to hold some data about a book. You would do something like this:

$book = new stdClass;
$book->title = "Harry Potter and the Prisoner of Azkaban";
$book->author = "J. K. Rowling";
$book->publisher = "Arthur A. Levine Books";
$book->amazon_link = "http://www.amazon.com/dp/0439136369/";

You can then access the data by calling $book->title and so on.

Or what if you wanted to take an array and turn it into an object? You can do that by casting the variable.

$array = array(
"title" => "Harry Potter and the Prisoner of Azkaban",
"author" => "J. K. Rowling",
"publisher" => "Arthur A. Levine Books",
"amazon_link" => "http://www.amazon.com/dp/0439136369/"
);

$books = (object) $array;

This should produce the same result as the previous code snippet. You could also go the other direction, casting an object into an array by using $array = (array) $object.

Objects are really useful when you’re working with a large data structure, as you can have an object with nested sub-arrays in it, as SimpleXML tends to return. You get most of the data as properties of the result set object, and the posts in an RSS feed are assigned to array entries, which then have further objects inside them.

  • Pingback: PHP stdClass: Storing Data in an Object Instead of an Array [ Webmaster-Source ]

  • Pingback: PHP stdClass: objetos en lugar de arrays | fprieto.es

  • Evandro Oliveira

    Nice article. Very easy comprehensible. TYVM

  • http://theelitist.net Xeross

    Seems to be an easy alternative to a registry class.

  • BobTheBuilder

    thanks for that! especially the 2nd time for casting arrays into objects.

  • Pingback: Compilado de enlaces útiles « droope.wordpress

  • http://www.Sistemas7g.com Desarrollo Web

    Thanks, I was searching for casting an object into an array by using $array = (array) $object.

  • enam

    Nice article but if any one need more then can check http://php.net/manual/en/language.oop5.basic.php link.

  • Neil Kolban

    Excellent article. Thank you.

  • http://www.fun-with-hdr.com Johannes

    Perfect. I googled this two times over the last week and luckily always landed on your site.

  • http://www.bouncehouse.biz Bounce House

    Very good demonstration of stdClass. One subtle difference between the stdClass and Object in other OOP is that stdClass is not a base class of object.

  • Simon

    Great article about that topic. Thank you very much.

  • nutz

    Nice explanation!thanks

  • Alok Ranjan

    Really nice explanation. Easy to understand. Thank you very much.

  • http://www.coursesweb.net OnLine

    Hi,
    Thank you for this article.
    I was looking for a way to create obects dinamically in PHP, with no class, and stdClass seems to be usefull for this.

  • http://www.bouncehouse.biz Bounce House

    I personally prefer working with Objects than arrays, and stdClass is a perfect alternative. Thanks for a good article.

  • http://shinephp.com vladimir

    I use this technique from time to time, as it is more convenient for me to address object property in the code, comparing to array element.
    I went to your blog via Google just to remember, how stdclass is named :).
    Thanks.

  • Prashant

    Thanks.Nice explaination.

  • Pingback: Adding a coating of civilization to PHP stdclass with the PGMObject base class : BioTeam

  • sazzlefrass

    Been looking for something like for array to object conversion! Thanks guy – Awesome!

  • Pingback: SEO obligations, php must turn json data into html | njaarts

  • http://muasamvui.com mua sam vui

    thansk ! i am processing a project and i need this !

  • bubu

    Work with arrays faster then with objects

    • bubu

      In php of course

  • jaffer

    Very nice article..
    Short but Covers THE Thing

    Thanks
    Jaffer

  • Sean Pinegar

    It’s worth mentioning that if you cast a multidimensional array as an object it only converts the top level to an object. Sub-arrays will still be arrays.

    To convert an entire multidimensional array to an object do the following:
    $obj = json_decode(json_encode($array));

  • Mike

    Good Article

  • Dion PHP

    really helpful

  • http://www.ortho.nl Michael Dibbets

    Thanks for the clear no nonsense approach.
    Too many sites are too much text. This is just simple for programmers to understand!.
    Love it.

  • http://www.mbinusolutions.com Mbinu Seo

    StdClass is a sparsely documented class in PHP which has no predefined members.

    $object = new StdClass;
    $object->foo = ‘xxx';
    json_encode($object);

    That was a super nice explanation and it was simple to understand too.

  • http://www.bizpress.co.il Lior

    I watched some tutorials and wanted to understand how the json_decode() and stdClass work, now I got it.

    Thanks for easy to understand article.

  • Pingback: PHP 建立物件來輸出 JSON 格式 - Tsung's Blog

  • Pingback: [php] structured array | PipisCrew Official Homepage

  • Shrinivasa

    Simply Explained :)

  • AK

    Thanks bro. This was a great explanation.