Skip to content

Allow passing a second argument to gettype to resolve class names #5142

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ function stream_set_timeout($socket, int $seconds, int $microseconds = 0): bool
/* type.c */

/** @param mixed $var */
function gettype($var): string {}
function gettype($var, bool $resolve_object_names = false): string {}

function settype(&$var, string $type): bool {}

Expand Down
1 change: 1 addition & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gettype, 0, 1, IS_STRING, 0)
ZEND_ARG_INFO(0, var)
ZEND_ARG_TYPE_INFO(0, resolve_object_names, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_settype, 0, 2, _IS_BOOL, 0)
Expand Down
28 changes: 28 additions & 0 deletions ext/standard/tests/general_functions/gettype_classes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Test gettype() class reading
--FILE--
<?php

class Test {

}

/* tests against an object type */
echo gettype(new Test()) . "\n";
echo gettype(new Test(), false) . "\n";
echo gettype(new Test(), true) . "\n";

/* tests against everything else */
echo gettype(1) . "\n";
echo gettype(1.1) . "\n";
echo gettype("foo") . "\n";
echo gettype(Test::class) . "\n";

--EXPECT--
object
object
Test
integer
double
string
string
9 changes: 8 additions & 1 deletion ext/standard/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ PHP_FUNCTION(gettype)
{
zval *arg;
zend_string *type;
zend_bool resolve_object_names = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(arg)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(resolve_object_names)
ZEND_PARSE_PARAMETERS_END();

if (resolve_object_names && Z_TYPE_P(arg) == IS_OBJECT) {
RETURN_STR_COPY(Z_OBJ_P(arg)->ce->name);
Copy link
Member

@kocsismate kocsismate Feb 2, 2020

Choose a reason for hiding this comment

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

One gotcha I see with this solution is the ambiguous result when a resource class instance is passed to get_type(). See https://3v4l.org/5aEd2 for example. I admit though that this is an edge case (and maybe resource should be reserved independently from this PR?).

}

type = zend_zval_get_type(arg);
if (EXPECTED(type)) {
RETURN_INTERNED_STR(type);
Expand Down