tony2001 Tue Sep 19 09:33:02 2006 UTC Added files: /php-src/ext/standard/tests/array array_walk_objects.phpt array_walk_rec_objects.phpt
Modified files: /php-src/ext/standard array.c /php-src/ext/standard/tests/array array_walk.phpt array_walk_recursive1.phpt Log: support objects in array_walk*() add new tests, fix old ones
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/array.c?r1=1.384&r2=1.385&diff_format=u Index: php-src/ext/standard/array.c diff -u php-src/ext/standard/array.c:1.384 php-src/ext/standard/array.c:1.385 --- php-src/ext/standard/array.c:1.384 Tue Sep 19 09:02:39 2006 +++ php-src/ext/standard/array.c Tue Sep 19 09:33:02 2006 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: array.c,v 1.384 2006/09/19 09:02:39 tony2001 Exp $ */ +/* $Id: array.c,v 1.385 2006/09/19 09:33:02 tony2001 Exp $ */ #include "php.h" #include "php_ini.h" @@ -1115,18 +1115,27 @@ *userdata = NULL; zend_fcall_info orig_array_walk_fci; zend_fcall_info_cache orig_array_walk_fci_cache; + HashTable *target_hash; orig_array_walk_fci = BG(array_walk_fci); orig_array_walk_fci_cache = BG(array_walk_fci_cache); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|z", &array, + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zf|z", &array, &BG(array_walk_fci), &BG(array_walk_fci_cache), &userdata) == FAILURE) { BG(array_walk_fci) = orig_array_walk_fci; BG(array_walk_fci_cache) = orig_array_walk_fci_cache; return; } - php_array_walk(HASH_OF(array), userdata ? &userdata : NULL, 0 TSRMLS_CC); + target_hash = HASH_OF(array); + if (!target_hash) { + BG(array_walk_fci) = orig_array_walk_fci; + BG(array_walk_fci_cache) = orig_array_walk_fci_cache; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array"); + RETURN_FALSE; + } + + php_array_walk(target_hash, userdata ? &userdata : NULL, 0 TSRMLS_CC); BG(array_walk_fci) = orig_array_walk_fci; BG(array_walk_fci_cache) = orig_array_walk_fci_cache; RETURN_TRUE; @@ -1141,17 +1150,26 @@ *userdata = NULL; zend_fcall_info orig_array_walk_fci; zend_fcall_info_cache orig_array_walk_fci_cache; + HashTable *target_hash; orig_array_walk_fci = BG(array_walk_fci); orig_array_walk_fci_cache = BG(array_walk_fci_cache); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|z", &array, + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zf|z", &array, &BG(array_walk_fci), &BG(array_walk_fci_cache), &userdata) == FAILURE) { BG(array_walk_fci) = orig_array_walk_fci; BG(array_walk_fci_cache) = orig_array_walk_fci_cache; return; } + target_hash = HASH_OF(array); + if (!target_hash) { + BG(array_walk_fci) = orig_array_walk_fci; + BG(array_walk_fci_cache) = orig_array_walk_fci_cache; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array"); + RETURN_FALSE; + } + php_array_walk(HASH_OF(array), userdata ? &userdata : NULL, 1 TSRMLS_CC); BG(array_walk_fci) = orig_array_walk_fci; BG(array_walk_fci_cache) = orig_array_walk_fci_cache; http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_walk.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_walk.phpt diff -u php-src/ext/standard/tests/array/array_walk.phpt:1.1 php-src/ext/standard/tests/array/array_walk.phpt:1.2 --- php-src/ext/standard/tests/array/array_walk.phpt:1.1 Thu Sep 7 11:47:57 2006 +++ php-src/ext/standard/tests/array/array_walk.phpt Tue Sep 19 09:33:02 2006 @@ -34,7 +34,7 @@ Warning: array_walk() expects at least 2 parameters, 0 given in %s on line %d NULL -Warning: array_walk() expects parameter 1 to be array, integer given in %s on line %d +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d NULL Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d @@ -52,7 +52,7 @@ Warning: array_walk() expects at least 2 parameters, 0 given in %s on line %d NULL -Warning: array_walk() expects parameter 1 to be array, integer given in %s on line %d +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d NULL Warning: array_walk() expects parameter 2 to be valid callback, Unicode string given in %s on line %d http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_walk_recursive1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_walk_recursive1.phpt diff -u php-src/ext/standard/tests/array/array_walk_recursive1.phpt:1.1 php-src/ext/standard/tests/array/array_walk_recursive1.phpt:1.2 --- php-src/ext/standard/tests/array/array_walk_recursive1.phpt:1.1 Thu Sep 7 11:47:57 2006 +++ php-src/ext/standard/tests/array/array_walk_recursive1.phpt Tue Sep 19 09:33:02 2006 @@ -34,7 +34,7 @@ Warning: array_walk_recursive() expects at least 2 parameters, 0 given in %s on line %d NULL -Warning: array_walk_recursive() expects parameter 1 to be array, integer given in %s on line %d +Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d NULL Warning: array_walk_recursive() expects parameter 2 to be valid callback, string given in %s on line %d @@ -58,7 +58,7 @@ Warning: array_walk_recursive() expects at least 2 parameters, 0 given in %s on line %d NULL -Warning: array_walk_recursive() expects parameter 1 to be array, integer given in %s on line %d +Warning: array_walk_recursive() expects parameter 2 to be valid callback, integer given in %s on line %d NULL Warning: array_walk_recursive() expects parameter 2 to be valid callback, Unicode string given in %s on line %d http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_walk_objects.phpt?view=markup&rev=1.1 Index: php-src/ext/standard/tests/array/array_walk_objects.phpt +++ php-src/ext/standard/tests/array/array_walk_objects.phpt --TEST-- array_walk() and objects --FILE-- <?php function walk($key, $value) { var_dump($value, $key); } class test { private $var_pri = "test_private"; protected $var_pro = "test_protected"; public $var_pub = "test_public"; } $stdclass = new stdclass; $stdclass->foo = "foo"; $stdclass->bar = "bar"; array_walk($stdclass, "walk"); $t = new test; array_walk($t, "walk"); $var = array(); array_walk($var, "walk"); $var = ""; array_walk($var, "walk"); echo "Done\n"; ?> --EXPECTF-- string(3) "foo" string(3) "foo" string(3) "bar" string(3) "bar" string(13) " string(12) "test_private" string(10) " string(14) "test_protected" string(7) "var_pub" string(11) "test_public" Warning: array_walk(): The argument should be an array in %s on line %d Done --UEXPECTF-- unicode(3) "foo" unicode(3) "foo" unicode(3) "bar" unicode(3) "bar" unicode(13) " unicode(12) "test_private" unicode(10) " unicode(14) "test_protected" unicode(7) "var_pub" unicode(11) "test_public" Warning: array_walk(): The argument should be an array in %s on line %d Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_walk_rec_objects.phpt?view=markup&rev=1.1 Index: php-src/ext/standard/tests/array/array_walk_rec_objects.phpt +++ php-src/ext/standard/tests/array/array_walk_rec_objects.phpt --TEST-- array_walk_recursive() and objects --FILE-- <?php function walk($key, $value) { var_dump($value, $key); } class test { private $var_pri = "test_private"; protected $var_pro = "test_protected"; public $var_pub = "test_public"; } $stdclass = new stdclass; $stdclass->foo = "foo"; $stdclass->bar = "bar"; array_walk_recursive($stdclass, "walk"); $t = new test; array_walk_recursive($t, "walk"); $var = array(); array_walk_recursive($var, "walk"); $var = ""; array_walk_recursive($var, "walk"); echo "Done\n"; ?> --EXPECTF-- string(3) "foo" string(3) "foo" string(3) "bar" string(3) "bar" string(13) " string(12) "test_private" string(10) " string(14) "test_protected" string(7) "var_pub" string(11) "test_public" Warning: array_walk_recursive(): The argument should be an array in %s on line %d Done --UEXPECTF-- unicode(3) "foo" unicode(3) "foo" unicode(3) "bar" unicode(3) "bar" unicode(13) " unicode(12) "test_private" unicode(10) " unicode(14) "test_protected" unicode(7) "var_pub" unicode(11) "test_public" Warning: array_walk_recursive(): The argument should be an array in %s on line %d Done
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php