php - stdClass

Development/PHP 2015. 6. 10. 17:21


Object Initialization ¶

To create a new object, use the new statement to instantiate a class:

<?php
class foo
{
    function 
do_foo()
    {
        echo 
"Doing foo."
    }
}

$bar = new foo;
$bar->do_foo();
?>

For a full discussion, see the Classes and Objects chapter.

Converting to object ¶

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClassbuilt-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.

<?php
$obj 
= (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(false)'
var_dump(key($obj)); // outputs 'int(1)'
?>

For any other value, a member variable named scalar will contain the value.

<?php
$obj 
= (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'
?>



source - http://php.net/manual/en/language.types.object.php








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:

1
2
3
4
5
$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.

1
2
3
4
5
6
7
8
$array = array(
"title" => "Harry Potter and the Prisoner of Azkaban",
"author" => "J. K. Rowling",
"publisher" => "Arthur A. Levine Books",
);
 
$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.



source - http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/



'Development > PHP' 카테고리의 다른 글

php - @ operator  (0) 2015.06.11
php - eclipse debugging  (0) 2015.06.04
php - eclipse zend debugger plugin install  (0) 2015.06.03
php - array  (0) 2013.06.19
php - foreach 문  (0) 2013.06.19
Posted by linuxism
,