# [PATCH] Allow get_declared_classes() and get_declared_interfaces() return user/internal class/interface separately

- Thread: https://externals.io/message/28635
- Original archive: https://news-web.php.net/php.internals/28635
- Started by Jani Taskinen on 2007-03-28T22:44:31+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28635 — Jani Taskinen — 2007-03-28T22:44:31+00:00

URL: https://externals.io/message/28635#28635

This patch allows get_declared_classes and `get_declared_interfaces()`
functions to return the information in associative array with "internal"
and "user" keys when passed the optional parameter. And makes them a bit
more consistent with similar functions like `get_defined_functions()`..

Example:

[jani@localhost php_5_2tst]$ sapi/cli/php -r 'class foobar {}
print_r(get_declared_classes(true));'|less
Array
(
     [internal] => Array
         (
             [0] => stdClass
             [1] => Exception
             [2] => ErrorException
             [3] => __PHP_Incomplete_Class
             [4] => php_user_filter
             [5] => Directory
             [6] => DateTime
             [7] => DateTimeZone
         )

     [user] => Array
         (
             [0] => foobar
         )

)

--Jani

---

## #28640 — Marcus Boerger — 2007-03-31T09:17:48+00:00

URL: https://externals.io/message/28635#28640 · In reply to: #28635

Hello Jani,

  how about allowing all those functions to return [internal/user], [user],
[internal] or [[internal],[user]] depending on a flag?

best regards
marcus

Thursday, March 29, 2007, 12:44:31 AM, you wrote:

> Index: zend_builtin_functions.c
> ===================================================================
> RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v
> retrieving revision 1.277.2.12.2.16
> diff -u -r1.277.2.12.2.16 zend_builtin_functions.c
> --- zend_builtin_functions.c    24 Feb 2007 02:17:23 -0000      1.277.2.12.2.16
> +++ zend_builtin_functions.c    28 Mar 2007 12:19:54 -0000
> @@ -1333,46 +1333,76 @@
>         zval *array = va_arg(args, zval *);
>         zend_uint mask = va_arg(args, zend_uint);
>         zend_uint comply = va_arg(args, zend_uint);
> -       zend_uint comply_mask = (comply)? mask:0;
> +       zend_uint comply_mask = (comply) ? mask : 0;
>         zend_class_entry *ce  = *pce;
>  
> -       if ((hash_key->nKeyLength==0 || hash_key->arKey[0]!=0)
> +       if ((hash_key->nKeyLength == 0 || hash_key->arKey[0] != 0)
>                 && (comply_mask == (ce->ce_flags & mask))) {
> -               add_next_index_stringl(array, ce->name, ce->name_length, 1);
> +               
> +               if (num_args == 3) {
> +                       add_next_index_stringl(array, ce->name, ce->name_length, 1);
> +               } else if (num_args == 5) {
> +                       zval *internal_ar = va_arg(args, zval *),
> +                            *user_ar     = va_arg(args, zval *);
> +
> +                       if (ce->type == ZEND_INTERNAL_CLASS) {
> +                               add_next_index_stringl(internal_ar, ce->name, ce->name_length, 1);
> +                       } else if (ce->type == ZEND_USER_CLASS) {
> +                               add_next_index_stringl(user_ar, ce->name, ce->name_length, 1);
> +                       }
> +               }
>         }
>         return ZEND_HASH_APPLY_KEEP;
>  }
>  
> -
> -/* {{{ proto array `get_declared_classes()`
> -   Returns an array of all declared classes. */
> -ZEND_FUNCTION(get_declared_classes)
> +static void
> do_get_declared_classes_or_interfaces(INTERNAL_FUNCTION_PARAMETERS, zend_uint mask, zend_uint comply)
>  {
> -       zend_uint mask = ZEND_ACC_INTERFACE;
> -       zend_uint comply = 0;
> +       zend_bool return_assoc = 0;
>  
> -       if (ZEND_NUM_ARGS() != 0) {
> -               ZEND_WRONG_PARAM_COUNT();
> +       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &return_assoc) == FAILURE) {
> +               return;
>         }
> -
> +                
>         array_init(return_value);
> -       zend_hash_apply_with_arguments(EG(class_table),
> (apply_func_args_t) copy_class_or_interface_name, 3, return_value, mask, comply);
> +       
> +       if (return_assoc)
> +       {
> +               zval *internal;
> +               zval *user;
> +
> +               MAKE_STD_ZVAL(internal);
> +               MAKE_STD_ZVAL(user);
> +
> +               array_init(internal);
> +               array_init(user);
> +               
> +               zend_hash_apply_with_arguments(EG(class_table),
> (apply_func_args_t) copy_class_or_interface_name, 5, return_value, mask, comply, internal, user);
> +               zend_hash_add(Z_ARRVAL_P(return_value), "internal",
> sizeof("internal"), (void **) &internal, sizeof(zval *), NULL);
> +               zend_hash_add(Z_ARRVAL_P(return_value), "user",
> sizeof("user"), (void **) &user, sizeof(zval *), NULL);
> +       } else {
> +               zend_hash_apply_with_arguments(EG(class_table),
> (apply_func_args_t) copy_class_or_interface_name, 3, return_value, mask, comply);
> +       }
>  }
>  /* }}} */
>  
> -/* {{{ proto array `get_declared_interfaces()`
> -   Returns an array of all declared interfaces. */
> +/* {{{ proto array get_declared_classes([bool return_assoc])
> +   Returns an array of declared classes. */
> +ZEND_FUNCTION(get_declared_classes)
> +{
> +       zend_uint mask = ZEND_ACC_INTERFACE;
> +       zend_uint comply = 0;
> +
> +      
> do_get_declared_classes_or_interfaces(INTERNAL_FUNCTION_PARAM_PASSTHRU, mask, comply);
> +}
> + 
> +/* {{{ proto array get_declared_interfaces([bool assoc])
> +   Returns an array of declared interfaces. */
>  ZEND_FUNCTION(get_declared_interfaces)
>  {
>         zend_uint mask = ZEND_ACC_INTERFACE;
>         zend_uint comply = 1;
>  
> -       if (ZEND_NUM_ARGS() != 0) {
> -               ZEND_WRONG_PARAM_COUNT();
> -       }
> -
> -       array_init(return_value);
> -       zend_hash_apply_with_arguments(EG(class_table),
> (apply_func_args_t) copy_class_or_interface_name, 3, return_value, mask, comply);
> +      
> do_get_declared_classes_or_interfaces(INTERNAL_FUNCTION_PARAM_PASSTHRU, mask, comply);
>  }
>  /* }}} */
>  
> Index: tests/017.phpt
> ===================================================================
> RCS file: /repository/ZendEngine2/tests/017.phpt,v
> retrieving revision 1.1.2.2
> diff -u -r1.1.2.2 017.phpt
> --- tests/017.phpt      21 Nov 2006 11:11:39 -0000      1.1.2.2
> +++ tests/017.phpt      28 Mar 2007 12:19:54 -0000
> @@ -28,7 +28,11 @@
>  var_dump(gettype(get_defined_functions()));
>  var_dump(count(get_defined_functions()));
>  
> -var_dump(get_declared_interfaces(true));
> +var_dump(get_declared_classes(true,true));
> +var_dump(gettype(get_declared_classes()));
> +var_dump(count(get_declared_classes()));
> +
> +var_dump(get_declared_interfaces(true,true));
>  var_dump(gettype(get_declared_interfaces()));
>  var_dump(count(get_declared_interfaces()));
>  
> @@ -67,7 +71,12 @@
>  string(5) "array"
>  int(%d)
>  
> -Warning: Wrong parameter count for `get_declared_interfaces()` in %s on line %d
> +Warning: `get_declared_classes()` expects at most 1 parameter, 2 given in %s on line %d
> +NULL
> +string(5) "array"
> +int(%d)
> +
> +Warning: `get_declared_interfaces()` expects at most 1 parameter, 2 given in %s on line %d
>  `NULL` 
>  string(5) "array"
>  int(%d)
> @@ -80,3 +89,4 @@
>  string(5) "array"
>  int(%d)
>  Done
> +




Best regards,
 Marcus
