0

Simple Way to Create PHP stdClass Objects

Simple Way to Create PHP stdClass Objects
Use type casting for create stdClass objects in PHP. Its convert simple array into a stdClass object. This is very quick and easy way to creating new stdClass objects with desired properties set.

Example to Create stdClass Objects in PHP

You must declare type cast objects before the array definition. check below PHP example to create stdClass object:

If you need to create stdClass object for associative array the use this code example:

Another case if you are going to use two array indexes with same key. Of course this is not allow to do, but what PHP do, Its just overwrites the property value the last key value in the array. Check the case below.

See ‘x’ key value overwrite with the last same key value. So this happen when you trying to use two array indexes with same key.

Here it is, quick and easy way to create stdclass object with desired properties value. Use this and share with others, Thanks

Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me to start working on your dream project - codefixup@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *