sebataz sebataz - 16 days ago 4
PHP Question

When creating my own classes in PHP should I extend stdClass?

While it is not required for a class in PHP to explicitly extend

stdClass
, I was wondering if there is some kind of best practice or benefit in doing so.

Should I extend
stdClass
when declaring a class?

For reference, see What is stdClass in PHP? and PHP: Objects, Converting to object.

Answer

StdClass is empty and therefore you don't have any benefit in extending it.

You are just wasting extend in your __CLASS__˙.

php --rc StdClass
Class [ <internal:Core> class stdClass ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [0] {
  }
}

The only reason to extend stdClass is to satisfy a typehint against this class - however, such a typehint should never be used, as such this is a moot point.