sebataz sebataz - 2 years ago 86
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

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

Should I extend
when declaring a class?

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download