PHP 4.0 Bug #4173: add function property_exists() please

From: Date: Tue, 18 Apr 2000 08:41:47 +0000
Subject: PHP 4.0 Bug #4173: add function property_exists() please
Groups: php.dev 
Request: Send a blank email to php-dev+get-17998@lists.php.net to get a copy of this message
From: dolecek@ics.muni.cz Operating system: NetBSD (1.4X kernel, 1.4.1 userland) PHP version: 4.0 Release Candidate 1 PHP Bug Type: Feature/Change Request Bug description: add function property_exists() please There are bunch of FOO_exists() functions to check for existence of functions, class methods and such. There is no way to check if an object has given attribute, though. It's easy to do in PHP: # return true if property exists in given object # boolean property_exists(Object obj, string name) function property_exists($obj, $name) { $attrs = get_object_vars($obj); return isset($attrs[$name]); } but this should be core PHP function. The C equivalent of above is in patch below, it even avoids copying the property array. Hopefully there are no other problems I overlooked. I have tested the code and everything seems ok and working. XXXXX --- Zend/zend_builtin_functions.c.orig Tue Apr 18 10:10:14 2000 +++ Zend/zend_builtin_functions.c Tue Apr 18 10:38:53 2000 @@ -40,6 +40,7 @@ static ZEND_FUNCTION(get_class); static ZEND_FUNCTION(get_parent_class); static ZEND_FUNCTION(method_exists); +static ZEND_FUNCTION(property_exists); static ZEND_FUNCTION(class_exists); static ZEND_FUNCTION(function_exists); static ZEND_FUNCTION(leak); @@ -74,6 +75,7 @@ ZEND_FE(get_class, NULL) ZEND_FE(get_parent_class, NULL) ZEND_FE(method_exists, NULL) + ZEND_FE(property_exists, NULL) ZEND_FE(class_exists, NULL) ZEND_FE(function_exists, NULL) ZEND_FE(leak, NULL) @@ -573,6 +575,32 @@ lcname = estrndup((*method_name)->value.str.val, (*method_name)->value.str.len); zend_str_tolower(lcname, (*method_name)->value.str.len); if(zend_hash_exists(&(*klass)->value.obj.ce->function_table, lcname, (*method_name)->value.str.len+1)) { + efree(lcname); + RETURN_TRUE; + } else { + efree(lcname); + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool property_exists(object object, string method) + Checks if the class attribute exists */ +ZEND_FUNCTION(property_exists) +{ + zval **obj, **prop_name; + char *lcname; + + if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &obj, &prop_name)==FAILURE) { + WRONG_PARAM_COUNT; + } + if ((*obj)->type != IS_OBJECT) { + RETURN_FALSE; + } + convert_to_string_ex(prop_name); + lcname = estrndup((*prop_name)->value.str.val, (*prop_name)->value.str.len); + zend_str_tolower(lcname, (*prop_name)->value.str.len); + if(zend_hash_exists((*obj)->value.obj.properties, lcname, (*prop_name)->value.str.len+1)) { efree(lcname); RETURN_TRUE; } else { XXXXX

« previous php.dev (#17998) next »