Skip to content

get_class() disallow null parameter RFC #2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ PHP NEWS
. Implemented request #69086 (enhancement for mb_convert_encoding() that
handles multibyte replacement char nicely) (Masakielastic, Yasuo)

- Standard:
. Implemented RFC: get_class() disallow null parameter. Passing null to
get_class() now generates a warning, rather than returning the calling
class scope.

<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

9 changes: 9 additions & 0 deletions Zend/tests/009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class foo {
function bar () {
var_dump(get_class());
}
function testNull ()
{
var_dump(get_class(null));
}
}

class foo2 extends foo {
Expand All @@ -27,6 +31,8 @@ var_dump(get_class("qwerty"));
var_dump(get_class($f1));
var_dump(get_class($f2));

$f1->testNull();

echo "Done\n";
?>
--EXPECTF--
Expand All @@ -45,4 +51,7 @@ Warning: get_class() expects parameter 1 to be object, string given in %s on lin
bool(false)
string(3) "foo"
string(4) "foo2"

Warning: get_class() expects parameter 1 to be object, null given in %s on line %d
bool(false)
Done
2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ ZEND_FUNCTION(get_class)
{
zval *obj = NULL;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the logic also be removed from source which handles obj == NULL at 1016 line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antanas-arvasevicius I don't think so. The parameter is still optional, and so calling get_class() returns the current scope, or gives a warning and returns false, if called from outside a class's scope.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sorry. Right, I've missed that get_class() could be still a valid call. But it's now similar effect as get_called_class(), couldn't get_called_class() work in any methods and return current class name then get_class($object); would do only one thing, returns a class name of a given object and no more else :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antanas-arvasevicius get_called_class is aware of the 'late static binding' aka the real class, not the current scope class, so they are not equivalent. https://3v4l.org/ISkEZ

RETURN_FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ bool(false)

Arg value: (type: NULL)

Warning: get_class() called without object from outside a class in %sget_class_variation_001.php on line %d
Warning: get_class() expects parameter 1 to be object, null given in %s on line %d
bool(false)

Arg value: (type: NULL)

Warning: get_class() called without object from outside a class in %sget_class_variation_001.php on line %d
Warning: get_class() expects parameter 1 to be object, null given in %s on line %d
bool(false)

Arg value: 1 (type: boolean)
Expand Down Expand Up @@ -202,11 +202,11 @@ bool(false)

Arg value: (type: NULL)

Warning: get_class() called without object from outside a class in %sget_class_variation_001.php on line %d
Warning: get_class() expects parameter 1 to be object, null given in %s on line %d
bool(false)

Arg value: (type: NULL)

Warning: get_class() called without object from outside a class in %sget_class_variation_001.php on line %d
Warning: get_class() expects parameter 1 to be object, null given in %s on line %d
bool(false)
Done