[PHP-DEV] PHP 4.0 Bug #4173: add function property_exists() please From: dolecek <email protected>
Date: 04/18/00

From: dolecek <email protected>
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

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>