4

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.

Community
  • 1
  • 1
sebataz
  • 985
  • 2
  • 10
  • 35
  • 2
    Nope. There is no need. – Anatoliy Kim Apr 13 '14 at 16:20
  • 1
    maybe at some point PHP will add some useful "utility" methods to stdclass,and your derived objects would suddenly have those utilities as well, but it's rather unlikely. – Marc B Apr 13 '14 at 16:22
  • @MarcB I personally think that'd be nice. For instance, some pseudo-operator overloading (with `isEqual`, `isGreater`, etc.) but for that to be effectively implemented types would need to automatically derive `stdClass`. – Dan Lugg Apr 13 '14 at 18:07

3 Answers3

14

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.

NikiC
  • 95,987
  • 31
  • 182
  • 219
Dejan Marjanović
  • 18,628
  • 7
  • 49
  • 65
  • 7
    There is one benefit to extend from stdClass. It will simply allow your class to have dynamic properties out of the box. Another way to achieve the same would be implement ArrayAccess and Countable interfaces. – Arman P. Jul 02 '14 at 22:29
  • 1
    @ArmanP. nearly all PHP objects can have dynamic properties without doing anything special. The only way to prevent this is if the object implements `__get` and `__set` and you throw an exception there. – Michael Butler May 11 '18 at 20:36
7

No           

Community
  • 1
  • 1
tereško
  • 56,151
  • 24
  • 92
  • 147
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Bhavik Ambani Apr 13 '14 at 16:58
  • 2
    @BhavikAmbani: Actually, it provides a perfectly clear, concise and correct answer to the question. No. – Madara's Ghost Apr 13 '14 at 17:00
  • I think better you mention this in comment section – Bhavik Ambani Apr 13 '14 at 17:01
  • @BhavikAmbani: Why? What makes this different from my answer? He asked whether he should extend stdClass, the answer is no. How is this a comment? – Madara's Ghost Apr 13 '14 at 17:02
  • Please look at Toly's comment, he has described this in comment section itself – Bhavik Ambani Apr 13 '14 at 17:03
  • 4
    Since you seem to be confused about it, @BhavikAmbani, lemme explain. **I was making a point.** Hell, I even made it CW to not get any rep from this non-answer on a non-question. – tereško Apr 13 '14 at 17:05
  • 5
    @BhavikAmbani: Just because the answer is short, doesn't mean it needs to be a comment. Read your automated comment for yourself. Is he "critiquing" or "request clarification from an author"? No, he does not. Please learn the difference between answer and comment before pasting your auto-comments on legitimate answers. – Madara's Ghost Apr 13 '14 at 17:05
1

There's one fun use to extend stdClass: ($object instanceof stdClass) will return true and is 3x faster than is_object($object). We're talking a 1 microsecond difference, though and unfortunately, unless you make it a strict policy throughout the project, it can easily become incoherent and thus more confusing than helping. In fact, I'm disappointed to learn that PHP classes don't extend stdClass by default. Would have made sense to me.

korkman
  • 1,707
  • 1
  • 10
  • 14