I work on a proprietary project that uses quite a lot of factories of one form or another. Most of them don't instantiate the class by name, fortunately, but whether new self()
or new static()
is used to instantiate varies depending on the developer.
I'm aware of the difference, but I'm curious if there's some consensus on which one is the "correct" way to go when late static binding isn't technically required. For instance, new static()
is often found in service classes that will almost certainly never be subclassed. It's obviously important in abstract classes, but my preference would be to use new self()
where I don't expect a subclass.
Questions that address the technical difference:
What I'm curious about:
- Is there a performance hit to using late static binding?
- Are there code maintenance implications to adopting one practice? Eg. If I subclass a class using
new self()
, I have to override/change all such cases, but maybe that's not a bad thing if my constructor changes. - Is there a documented best practice on this? We use PSR-2, at least aspirationally, but I don't think it covers this.