Take the State of PHP 2026 Survey →

get_class() disallow null parameter View as Markdown

Implemented (PHP 7.2)
Authors
Danack
Date
2016-08-12
Version
1.0
Source
wiki.php.net/rfc/get_class_disallow_null_parameter

Vote

get_class() disallow null parameter closed
Yes
15
No
3
18 voters
ajf No 2016-09-25 17:37
bishop Yes 2016-09-24 17:57
bwoebi Yes 2016-09-24 11:29
colinodell Yes 2016-09-24 20:28
danack Yes 2016-09-24 10:59
daverandom Yes 2016-09-24 11:18
galvao Yes 2016-09-29 03:57
kguest Yes 2016-10-05 13:03
leigh Yes 2016-10-06 08:47
lstrojny Yes 2016-09-28 10:05
malukenho No 2016-10-06 05:46
mariano Yes 2016-09-24 14:44
mike No 2016-09-26 14:17
mrook Yes 2016-09-26 07:50
ocramius Yes 2016-09-24 11:55
peehaa Yes 2016-09-24 13:38
stas Yes 2016-09-25 04:30
trowski Yes 2016-09-27 15:02

Discussions on the mailing list

[RFC] get_class() disallow null parameter
by Dan Ackroyd · 9 messages
[VOTE] get_class() disallow null parameter
by Dan Ackroyd · 12 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

When null is passed as the parameter to get_class() inside a class context, the behaviour of the function can be highly surprising,

class Foo
{
    function bar($repository)
    {
        $result = $repository->find(100);
 
        echo get_class($result);
    }
}

If $result contains a valid object returned from the repository, the output will be of the class name of the type of $result.

If $result contains null, the output will be of the class context where get_class() was called from, in this case “Foo”.

This feature violates the Principle of least astonishment: “if a necessary feature has a high astonishment factor, it may be necessary to redesign the feature.”

Proposal

Disallow null being passed to the function as a valid parameter. 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

I.e. the valid ways to call the function will be:

  • without any parameter.
  • with an object as the parameter.

Those two options will continue to have the same behaviour they currently have.

Backward Incompatible Changes

Most people won't see a BC break, as for the majority of people, passing null to this function is not a desired behaviour.

For people who do deliberately pass null to the function they will need to refactor their code from:

$x = get_class($some_value_that_may_be_null);

to:

if ($some_value_that_may_be_null === null) {
    $x = get_class();
}
else {
    $x = get_class($some_value_that_may_be_null);
}

Proposed PHP Version(s)

7.2

Voting

Should the get_class() function be changed to disallow null being passed as a parameter? As it is not a language or syntax change, the vote will pass if 50%+1 vote yes.

Vote: get_class() disallow null parameter — Yes: 15 · No: 3 (closed)

Voting ended on the 8th October 2016 9pm UTC

null vs default param

When this topic was discussed before, it came as a surprise to some people that PHP can tell the difference between passing null and having a default value be null. This is perfectly possible in both internal code, as well as userland code:

function get_class($item = null)
{
    if (func_num_args() == 0) {
        return get_current_scope_name();
    }
    if ($item === null) {
        trigger_error("get_class passed null, which is not an object.");
    }
 
    if (is_object($item) == false) {
        trigger_error("value is not an object");
        return false;
    }
 
    return gettype($item);
}

Patches and Tests

https://github.com/php/php-src/pull/2082

Implementation

After the project is implemented, this section should contain

  1. This was merged for PHP 7.2
  2. https://github.com/php/php-src/pull/2082/commits/86aa1c784d3b584e6d0c235870a882c8fd169a74
  3. https://secure.php.net/manual/en/function.get-class.php#refsect1-function.get-class-changelog