|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2000-04-18 09:22 UTC] andrei at cvs dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 09:00:01 2025 UTC |
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