diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index a168069cfb623..c123988d987ac 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2549,6 +2549,19 @@ 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_to_int, 0, 0, 1) + ZEND_ARG_INFO(0, from) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_to_float, 0, 0, 1) + ZEND_ARG_INFO(0, from) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_to_string, 0, 0, 1) + ZEND_ARG_INFO(0, from) +ZEND_END_ARG_INFO() + /* }}} */ /* {{{ uniqid.c */ #ifdef HAVE_GETTIMEOFDAY @@ -3043,6 +3056,14 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(is_scalar, arginfo_is_scalar) PHP_FE(is_callable, arginfo_is_callable) + PHP_FE(to_int, arginfo_to_int) + PHP_FE(try_int, arginfo_to_int) + PHP_FE(to_float, arginfo_to_float) + PHP_FE(try_float, arginfo_to_float) + PHP_FE(to_string, arginfo_to_string) + PHP_FE(try_string, arginfo_to_string) + + /* functions from file.c */ PHP_FE(pclose, arginfo_pclose) PHP_FE(popen, arginfo_popen) @@ -3612,6 +3633,7 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */ BASIC_MINIT_SUBMODULE(standard_filters) BASIC_MINIT_SUBMODULE(user_filters) BASIC_MINIT_SUBMODULE(password) + BASIC_MINIT_SUBMODULE(type) #if defined(HAVE_LOCALECONV) && defined(ZTS) BASIC_MINIT_SUBMODULE(localeconv) diff --git a/ext/standard/php_type.h b/ext/standard/php_type.h index e6d8152b1be7d..acb809eebb0af 100644 --- a/ext/standard/php_type.h +++ b/ext/standard/php_type.h @@ -21,6 +21,8 @@ #ifndef PHP_TYPE_H #define PHP_TYPE_H +extern PHPAPI zend_class_entry *php_CastException_ce; + PHP_FUNCTION(intval); PHP_FUNCTION(floatval); PHP_FUNCTION(strval); @@ -38,5 +40,11 @@ PHP_FUNCTION(is_array); PHP_FUNCTION(is_object); PHP_FUNCTION(is_scalar); PHP_FUNCTION(is_callable); +PHP_FUNCTION(to_int); +PHP_FUNCTION(try_int); +PHP_FUNCTION(to_float); +PHP_FUNCTION(try_float); +PHP_FUNCTION(to_string); +PHP_FUNCTION(try_string); #endif diff --git a/ext/standard/tests/type/to_float.phpt b/ext/standard/tests/type/to_float.phpt new file mode 100644 index 0000000000000..d39b66ff26c46 --- /dev/null +++ b/ext/standard/tests/type/to_float.phpt @@ -0,0 +1,121 @@ +--TEST-- +to_float() +--FILE-- +cast_object) { + if (Z_OBJ_HT_P(var)->cast_object(var, out, IS_STRING TSRMLS_CC) == SUCCESS) { + return 1; + } + } else if (Z_OBJ_HT_P(var)->get) { + zval *newop = Z_OBJ_HT_P(var)->get(var, out TSRMLS_CC); + if (Z_TYPE_P(newop) != IS_OBJECT) { + /* for safety - avoid loop */ + ZVAL_COPY_VALUE(out, newop); + convert_to_string(out); + + return 1; + } + } + } + return 0; + + default: + return 0; + } +} + +/* {{{ proto string to_string(mixed from) + Strictly convert the given value to a string, throwing an exception on failure. */ +PHP_FUNCTION(to_string) +{ + zval *var; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) { + RETURN_NULL(); + } + + if (!safe_cast_to_string(var, return_value TSRMLS_CC)) { + zend_throw_exception(php_CastException_ce, "Value could not be converted to string", 0 TSRMLS_CC); + } +} +/* }}} */ + +/* {{{ proto string try_string(mixed from) + Strictly convert the given value to a string, returning NULL on failure. */ +PHP_FUNCTION(try_string) +{ + zval *var; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) { + RETURN_NULL(); + } + + if (!safe_cast_to_string(var, return_value TSRMLS_CC)) { + RETURN_NULL(); + } + +} +/* }}} */ + /* * Local variables: * tab-width: 4