Home / Articles / Programming languages / Scripting languages / PHP / What is stdClass and how to work with it in PHP

What is stdClass and how to work with it in PHP

What is stdClass and how to work with it in PHP
 
What is stdClass and how to work with it in PHP
  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 5/5 (2 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


stdClass is a default object which doesn't have predefined properties like a usual custom class. The name stdClass is used inside Zend and it's reserved. It means you can't define class using the name stdClass.

It can be used to create instances of objects then specific properties can set dynamically without predefining it. For example to pass an object to other function or method which expect to receive an object as an argument.

An example of use:

$person = new stdClass;
$person->email = 'example@example.com';
$person->firstName = 'FirstName';
$person->secondName = 'SecondName';
var_dump( $person );

Result:

object(stdClass)[1]
  public 'email' => string 'example@example.com' (length=19)
  public 'firstName' => string 'FirstName' (length=9)
  public 'secondName' => string 'SecondName' (length=10)

 

Read also

Classes and objects in PHP

Classes and objects in PHP

How to access classes and objects in PHP

How to access classes and objects in PHP

What is PHP?

What is PHP?

Classes and objects in PHP: constructors

Classes and objects in PHP: constructors

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation