# [RFC] get_class() disallow null parameter

- Thread: https://externals.io/message/95137
- Original archive: https://news-web.php.net/php.internals/95137
- Started by Dan Ackroyd on 2016-08-14T11:00:15+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #95137 — Dan Ackroyd — 2016-08-14T11:00:15+00:00

URL: https://externals.io/message/95137#95137

Hi!

Following an earlier discussion in January, here is a small RFC to
change `get_class()` to disallow null being passed as parameter.

https://wiki.php.net/rfc/get_class_disallow_null_parameter

Allowing null to be passed to `get_class()` is a 'gotcha' that is almost
always a mistake in someone's code.

By preventing null being passed, `get_class()` will be far more likely
to do what people think it should do, and so result in fewer bugs in
code.

This RFC is a small change and so is appropriate for a point release.
Other ways of making this function behave as people expect it to are
not as small changes, and are probably not appropriate for point
releases.

cheers
Dan
Ack

---

## #95142 — Marco Pivetta — 2016-08-14T18:01:10+00:00

URL: https://externals.io/message/95137#95142 · In reply to: #95137

Hi Dan,



> Hi!
>
> Following an earlier discussion in January, here is a small RFC to
> change `get_class()` to disallow null being passed as parameter.
>
> https://wiki.php.net/rfc/get_class_disallow_null_parameter
>
> Allowing null to be passed to `get_class()` is a 'gotcha' that is almost
> always a mistake in someone's code.
>
> By preventing null being passed, `get_class()` will be far more likely
> to do what people think it should do, and so result in fewer bugs in
> code.
>
> This RFC is a small change and so is appropriate for a point release.
> Other ways of making this function behave as people expect it to are
> not as small changes, and are probably not appropriate for point
> releases.
>

Thanks for bringing up this: every scenario where `null` has been passed to
`get_class()` was a bug, in my work context, so I'm really happy to see
this patch going in.

Still, can any of the release managers throw their opinion at the RFC?

This is a SemVer violation, but it doesn't seem like PHP follows SemVer
closely, so I'm a bit confused about whether this can target 7.x.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #95150 — Stanislav Malyshev — 2016-08-14T21:48:49+00:00

URL: https://externals.io/message/95137#95150 · In reply to: #95137

Hi!

> Following an earlier discussion in January, here is a small RFC to
> change `get_class()` to disallow null being passed as parameter.
> 
> https://wiki.php.net/rfc/get_class_disallow_null_parameter

Could you please clarify what you mean by "disallow"? I've read the RFC
but I didn't find description of what happens in that case.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #95152 — Dan Ackroyd — 2016-08-14T22:14:58+00:00

URL: https://externals.io/message/95137#95152 · In reply to: #95150

>
> Could you please clarify what you mean by "disallow"?

It will be the standard behaviour for accepting params. The patch is just:

- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &obj) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {


but I have added a clear description to the RFC:

> If `get_class()` is called with null as the parameter, a warning will be emitted:
>
> Warning: `get_class()` expects parameter 1 to be object, null given in %s on line %d

cheers
Dan

---

## #95156 — Christoph M. Becker — 2016-08-14T23:34:06+00:00

URL: https://externals.io/message/95137#95156 · In reply to: #95137

> Following an earlier discussion in January, here is a small RFC to
> change `get_class()` to disallow null being passed as parameter.
> 
> https://wiki.php.net/rfc/get_class_disallow_null_parameter
> 
> Allowing null to be passed to `get_class()` is a 'gotcha' that is almost
> always a mistake in someone's code.
> 
> By preventing null being passed, `get_class()` will be far more likely
> to do what people think it should do, and so result in fewer bugs in
> code.
> 
> This RFC is a small change and so is appropriate for a point release.
> Other ways of making this function behave as people expect it to are
> not as small changes, and are probably not appropriate for point
> releases.

Prohibiting `get_class(NULL)` is certainly a good idea, but I have some
concerns regarding BC.  While `__CLASS__` has been introduced with PHP
4.3.0, it had the glitch to return the lower-cased class name before PHP
5.0.0.  So there *might* still be PHP 5 code around using `get_class(NULL)`.

The deprecation might be more appropriate, and could equally well catch
an inadvertent `get_class(NULL)`.

-- 
Christoph M. Becker

---

## #95158 — Stanislav Malyshev — 2016-08-15T00:09:35+00:00

URL: https://externals.io/message/95137#95158 · In reply to: #95156

Hi!

> Prohibiting `get_class(NULL)` is certainly a good idea, but I have some
> concerns regarding BC.  While `__CLASS__` has been introduced with PHP
> 4.3.0, it had the glitch to return the lower-cased class name before PHP
> 5.0.0.  So there *might* still be PHP 5 code around using `get_class(NULL)`.

I note that this (i.e. behavior proposed in the RFC) was briefly the
case for 5.3.0 and was changed to current behavior for 5.3.1. But
pre-5.3 versions return false when `NULL` is passed to get_class, with no
message.

So, old PHP code doesn't seem to be a problem only one that was written
after 5.3.1, because it doesn't look like get_class(NULL) worked before
it: https://3v4l.org/4QvCs

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #95181 — Christoph M. Becker — 2016-08-15T10:38:17+00:00

URL: https://externals.io/message/95137#95181 · In reply to: #95158

> > […]
> 
> I note that this (i.e. behavior proposed in the RFC) was briefly the
> case for 5.3.0 and was changed to current behavior for 5.3.1. But
> pre-5.3 versions return false when `NULL` is passed to get_class, with no
> message.
> 
> So, old PHP code doesn't seem to be a problem only one that was written
> after 5.3.1, because it doesn't look like get_class(NULL) worked before
> it: https://3v4l.org/4QvCs

Thanks for pointing this out.  Interestingly, indicated by a changelog
entry in the `get_class()` manual, the behavior is somewhat different when
no argument is passed, see <https://3v4l.org/sWX9X>.  That works as
least as of PHP 5.2.0; not sure why 5.0 and 5.1 fail here.

As the proposed behavior had been introduced with 5.3.0 and reverted
shortly after, indicates that we should be careful here.  Deprecating
for 7.2 and removing with 8.0 seems to be the safer way.

-- 
Christoph M. Becker

---

## #95207 — Dan Ackroyd — 2016-08-15T20:17:10+00:00

URL: https://externals.io/message/95137#95207 · In reply to: #95142

> Hi Dan,
>
> This is a SemVer violation, but it doesn't seem like PHP follows SemVer
> closely, so I'm a bit confused about whether this can target 7.x.

It was broken in a minor release, it can be fixed in a minor release imo.

cheers
Dan

---

## #95209 — Marco Pivetta — 2016-08-15T20:30:24+00:00

URL: https://externals.io/message/95137#95209 · In reply to: #95207

Works for me 👍



> 
> > […]
>
> It was broken in a minor release, it can be fixed in a minor release imo.
>
> cheers
> Dan
