Skip to content
Closed

typeof #1932

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
13 changes: 13 additions & 0 deletions Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ struct _zend_ast_ref {
zend_ast *ast;
};

/* primitive data type names */
#define TYPE_ARRAY "array"
#define TYPE_BOOL "bool"
#define TYPE_CALLABLE "callable"
#define TYPE_FALSE "false"
#define TYPE_FLOAT "float"
#define TYPE_INT "int"
#define TYPE_NULL "null"
#define TYPE_OBJECT "object"
#define TYPE_RESOURCE "resource"
#define TYPE_STRING "string"
#define TYPE_TRUE "true"

/* regular data types */
#define IS_UNDEF 0
#define IS_NULL 1
Expand Down
10 changes: 9 additions & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "zend_operators.h"
#include "ext/standard/php_dns.h"
#include "ext/standard/php_uuencode.h"
#include "php_type.h"

#ifdef PHP_WIN32
#include "win32/php_win32_globals.h"
Expand Down Expand Up @@ -2563,6 +2564,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_is_callable, 0, 0, 1)
ZEND_ARG_INFO(0, syntax_only)
ZEND_ARG_INFO(1, callable_name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_typeof, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, var)
ZEND_ARG_INFO(0, extended)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ uniqid.c */
#ifdef HAVE_GETTIMEOFDAY
Expand Down Expand Up @@ -3060,6 +3066,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(is_object, arginfo_is_object)
PHP_FE(is_scalar, arginfo_is_scalar)
PHP_FE(is_callable, arginfo_is_callable)
PHP_FE(typeof, arginfo_typeof)

/* functions from file.c */
PHP_FE(pclose, arginfo_pclose)
Expand Down Expand Up @@ -3620,6 +3627,7 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
register_phpinfo_constants(INIT_FUNC_ARGS_PASSTHRU);
register_html_constants(INIT_FUNC_ARGS_PASSTHRU);
register_string_constants(INIT_FUNC_ARGS_PASSTHRU);
register_type_constants(INIT_FUNC_ARGS_PASSTHRU);

BASIC_ADD_SUBMODULE(dl)
BASIC_ADD_SUBMODULE(mail)
Expand Down Expand Up @@ -3998,7 +4006,7 @@ PHP_FUNCTION(long2ip)
********************/

/* {{{ proto string getenv([string varname])
Get the value of an environment variable or every available environment variable
Get the value of an environment variable or every available environment variable
if no varname is present */
PHP_FUNCTION(getenv)
{
Expand Down
5 changes: 4 additions & 1 deletion ext/standard/php_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ PHP_FUNCTION(is_array);
PHP_FUNCTION(is_object);
PHP_FUNCTION(is_scalar);
PHP_FUNCTION(is_callable);
PHP_FUNCTION(typeof);

#endif
void register_type_constants(INIT_FUNC_ARGS);

#endif /* PHP_TYPE_H */
38 changes: 38 additions & 0 deletions ext/standard/tests/type/type_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
TYPE_* constants are defined with their correct value
--DESCRIPTION--
This test ensures that users of PHP can rely on the definition of these
constants as well as their actual values and are meant to prevent any kind of
breaking changes in relation to these constants.
--FILE--
<?php

$data = [
'TYPE_ARRAY' => 'array',
'TYPE_BOOL' => 'bool',
'TYPE_CALLABLE' => 'callable',
'TYPE_FLOAT' => 'float',
'TYPE_INT' => 'int',
'TYPE_NULL' => 'null',
'TYPE_OBJECT' => 'object',
'TYPE_RESOURCE' => 'resource',
'TYPE_STRING' => 'string',
];

foreach ($data as $name => $value) {
if (defined($name) && constant($name) === $value) {
printf("%-13s := %s\n", $name, $value);
}
}

?>
--EXPECT--
TYPE_ARRAY := array
TYPE_BOOL := bool
TYPE_CALLABLE := callable
TYPE_FLOAT := float
TYPE_INT := int
TYPE_NULL := null
TYPE_OBJECT := object
TYPE_RESOURCE := resource
TYPE_STRING := string
11 changes: 11 additions & 0 deletions ext/standard/tests/type/typeof_TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Php\Test\Typeof;

class TestClass {

public static function staticMethod() {}

public function instanceMethod() {}

}
54 changes: 54 additions & 0 deletions ext/standard/tests/type/typeof_basic_arrays.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
typeof() correctly recognizes array vars
--FILE--
<?php

use Php\Test\Typeof\TestClass;

require __DIR__ . '/typeof_TestClass.php';

$data = [
[],
[null],
[4, 2],
['d' => 'i', 'c' => 't'],
[TestClass::class, 'staticMethod'],
[TestClass::class, 'instanceMethod'],
[new TestClass, 'staticMethod'],
[new TestClass, 'instanceMethod'],
];

echo "\n*** typeof test: array ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum));
}

echo "\n*** typeof test extended: array ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum, true));
}

?>
--EXPECTF--

*** typeof test: array ***
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"

*** typeof test extended: array ***
string(5) "array"
string(5) "array"
string(5) "array"
string(5) "array"
string(14) "callable array"

Deprecated: Non-static method Php\Test\Typeof\TestClass::instanceMethod() should not be called statically in %s on line %d
string(14) "callable array"
string(14) "callable array"
string(14) "callable array"
36 changes: 36 additions & 0 deletions ext/standard/tests/type/typeof_basic_bool.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
typeof() correctly recognizes bool vars
--FILE--
<?php

$data = [
false,
FALSE,
true,
TRUE,
];

echo "\n*** typeof test: bool ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum));
}

echo "\n*** typeof test extended: bool ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum, true));
}

?>
--EXPECTF--

*** typeof test: bool ***
string(4) "bool"
string(4) "bool"
string(4) "bool"
string(4) "bool"

*** typeof test extended: bool ***
string(10) "bool false"
string(10) "bool false"
string(9) "bool true"
string(9) "bool true"
102 changes: 102 additions & 0 deletions ext/standard/tests/type/typeof_basic_float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
--TEST--
typeof() correctly recognizes float vars
--FILE--
<?php

$data = [
-NAN,
-INF,
-.512E+6,
-.512E6,
-.5e+6,
-.5e-6,
-.5e6,
-1E5,
-1e5,
-1.5,
-1.0,
-.5,
-0.0,
0.0,
.5,
1.0,
1e5,
1E5,
.5e6,
.5e+6,
.512E6,
.512E-6,
.512E+6,
+.512E-6,
INF,
NAN,
];

echo "\n*** typeof test: float ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum));
}

echo "\n*** typeof test extended: float ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum, true));
}

?>
--EXPECTF--

*** typeof test: float ***
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"
string(5) "float"

*** typeof test extended: float ***
string(13) "invalid float"
string(14) "infinite float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(14) "negative float"
string(10) "zero float"
string(10) "zero float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "positive float"
string(14) "infinite float"
string(13) "invalid float"
60 changes: 60 additions & 0 deletions ext/standard/tests/type/typeof_basic_int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
typeof() correctly recognizes int vars
--FILE--
<?php

$data = [
PHP_INT_MIN,
-001,
-0x1,
-0b1,
-1,
-0,
0,
1,
0b1,
0x1,
001,
PHP_INT_MAX,
];

echo "\n*** typeof test: int ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum));
}

echo "\n*** typeof test extended: int ***\n";
foreach ($data as $datum) {
var_dump(typeof($datum, true));
}

?>
--EXPECTF--

*** typeof test: int ***
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"
string(3) "int"

*** typeof test extended: int ***
string(12) "negative int"
string(12) "negative int"
string(12) "negative int"
string(12) "negative int"
string(12) "negative int"
string(8) "zero int"
string(8) "zero int"
string(12) "positive int"
string(12) "positive int"
string(12) "positive int"
string(12) "positive int"
string(12) "positive int"
Loading