PHP arrays converted to Objects Php-convert array to object with stdclass_php tutorial

Source: Internet
Author: User
Tags type casting
The PHP StdClass () is something this isn ' t well documented but I'll try to shed some light into the matter. StdClass is a default PHP object which have no predefined members. The name StdClass is used internally by Zend and is reserved. So, means, cannot define a class named StdClass in your PHP code.

It can used to manually instantiate generic objects which you can then set member variables for, this is useful for PAS Sing objects to and functions or methods which expect to take a object as an argument. An even more likely usage was casting an array to an object which takes each value in the array and adds it as a member Var Iable with the name based on the key in the array.

Here's an example below this converts an array to an object below. This method is called Type Casting.

$person = Array (

' FirstName ' = ' Richard ',

' LastName ' = ' Castera '

);

$p = (object) $person;

Echo $p->firstname; Would print ' Richard '

?>

http://www.bkjia.com/PHPjc/478580.html www.bkjia.com true http://www.bkjia.com/PHPjc/478580.html techarticle The PHP StdClass () is something, isnt well documented but I'll try to shed some light into the matter. StdClass is a default PHP object which have no predefined members. The N ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.