Now Playing

PHP and stdClass

The built in class stdClass can be instantiated manually for creating generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.

<?php
$obj = (object) array("foo" => "bar")
echo $obj->foo; // outputs "bar"
?>

One place you might get caught out here is trying to directly access numerically indexed values on the object, this breaks PHP’s naming convention and does not work correctly even with escaping.

<?php
$obj = (object) array("foo"=>"bar","foobar");
echo $obj->foo; //outputs "bar"
echo $obj->{0}; //$x->0 would cause a syntax error, this simply causes a notice for being unable to find the variable.

No related posts.

One Response to “PHP and stdClass”

  1. small business Says:

    really cool stuff.

Leave a Reply

About

A Web Developer by nature and by trade. Living in Dublin, Ireland and working at Web Reservations International. Enjoys working on Open Source, web server stacks, XHTML/CSS, JavaScript and playing with the latest trends in technology.