Skip to content

Commit 90f92c8

Browse files
author
Your Name
committed
Allow resolving class names with gettype
1 parent 4cbffd8 commit 90f92c8

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ function stream_set_timeout($socket, int $seconds, int $microseconds = 0): bool
13151315
/* type.c */
13161316

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

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

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,7 @@ ZEND_END_ARG_INFO()
20082008

20092009
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gettype, 0, 1, IS_STRING, 0)
20102010
ZEND_ARG_INFO(0, var)
2011+
ZEND_ARG_TYPE_INFO(0, resolve_object_names, _IS_BOOL, 0)
20112012
ZEND_END_ARG_INFO()
20122013

20132014
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_settype, 0, 2, _IS_BOOL, 0)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Test gettype() class reading
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
8+
}
9+
10+
/* tests against an object type */
11+
echo gettype(new Test()) . "\n";
12+
echo gettype(new Test(), false) . "\n";
13+
echo gettype(new Test(), true) . "\n";
14+
15+
/* tests against everything else */
16+
echo gettype(1) . "\n";
17+
echo gettype(1.1) . "\n";
18+
echo gettype("foo") . "\n";
19+
echo gettype(Test::class) . "\n";
20+
21+
--EXPECTF--

ext/standard/type.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ PHP_FUNCTION(gettype)
2323
{
2424
zval *arg;
2525
zend_string *type;
26+
zend_bool resolve_object_names = 0;
27+
zend_object *obj;
2628

27-
ZEND_PARSE_PARAMETERS_START(1, 1)
29+
ZEND_PARSE_PARAMETERS_START(1, 2)
2830
Z_PARAM_ZVAL(arg)
31+
Z_PARAM_OPTIONAL
32+
Z_PARAM_BOOL(resolve_object_names)
2933
ZEND_PARSE_PARAMETERS_END();
3034

35+
if (resolve_object_names && Z_TYPE_P(arg) == IS_OBJECT) {
36+
obj = Z_OBJ_P(arg);
37+
RETURN_INTERNED_STR(obj->ce->name);
38+
}
39+
3140
type = zend_zval_get_type(arg);
3241
if (EXPECTED(type)) {
3342
RETURN_INTERNED_STR(type);

0 commit comments

Comments
 (0)