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

3

6

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.

Zwart answered 13/4, 2014 at 16:18 Comment(3)
Nope. There is no need.Ambitious
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.Discoverer
@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.Hellbender
M
15

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.

Monkfish answered 13/4, 2014 at 17:55 Comment(4)
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.Snelling
@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.Emma
Dynamic property names will be deprecated in PHP 8.2, with very few exceptions, and one of those is the stdClass class, to which you can add whatever you want as a property after initializing it. This would be a use for extending that.Shipshape
@Shipshape not much of a good idea though. Might be useful in some very specific scenarios, but not as a base for every class. Dynamic properties are being deprecated for a reason.Longspur
P
8

No           

Prajna answered 13/4, 2014 at 16:18 Comment(7)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Viscus
@BhavikAmbani: Actually, it provides a perfectly clear, concise and correct answer to the question. No.Inflight
I think better you mention this in comment sectionViscus
@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?Inflight
Please look at Toly's comment, he has described this in comment section itselfViscus
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.Amorous
@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.Inflight
E
4

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.

Ebracteate answered 22/11, 2016 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.