What is the purpose of stdClass in PHP?

Asked

Viewed 8,659 times

9

  • What is the purpose of the predefined class stdClass in PHP?
  • In detail what he does?
  • What is its importance?

1 answer

13


Stdclass is a predefined PHP class. It is empty, meaning it has no methods or properties. But what is the point of this? It is the default class of undeclared objects, that is, when you convert an array or some other type to an object, you are actually creating an Stdclass object. It is also useful to use Stdclass when you want to create an empty object and add the properties as needed.

An example of using Stdclass:

$obj = new StdClass;

$obj->nome = 'teste';

var_dump($obj);

Source

  • Do you create direct? type as an array? Example: $arr = array('nome' => 'teste', ...) thus: $obj = new stdClass('nome' => 'teste')

  • You need to instantiate it before passing the value.

  • standardClass...

  • 2

    @Joseph, doing a Typecast, example: $o = (object)array('foo' => 'bar'); You can then access it this way $o->foo or even create new properties $o->outro = 'valor';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.