php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #4173 add function property_exists() please
Submitted: 2000-04-18 04:41 UTC Modified: 2000-04-18 09:22 UTC
From: dolecek at ics dot muni dot cz Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0 Release Candidate 1 OS: NetBSD (1.4X kernel, 1.4.1 userl
Private report: No CVE-ID: None
 [2000-04-18 04:41 UTC] dolecek at ics dot muni dot cz
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-04-18 09:22 UTC] andrei at cvs dot php dot net
Use isset($obj->$name) instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Feb 25 09:01:26 2024 UTC