Re: Calling symbol table patch

From: Date: Mon, 20 Nov 2000 21:35:48 +0000
Subject: Re: Calling symbol table patch
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.dev 
Request: Send a blank email to php-dev+get-38700@lists.php.net to get a copy of this message
At 03:35 PM 11/20/2000 -0600, Andrei Zmievski wrote:
On Mon, 20 Nov 2000, Zeev Suraski wrote: But much more correct. Giving access to the calling function symbol table, without the calling function explicitly 'giving permission' to do it (in the form of passing its symbol table as an argument), breaks a fundamental rule of scope in structured programming... We all know that there are some cases in which a specialized function would be a serious shortcut, but would be the wrong thing to do. I think that's such a case. Then it should be easy to pass the symbol table (macros would be a way to do it, but who knows how far off that is).
Below the message is the function, formatting sucks, I'll send Zeev and Andi a diff with correct formatting once everything gets worked out around this. It basically returns scope information for passing along... You also have the choice of what you want to pass (ie, globals, current or both): Andrei: $scope = get_defined_vars("current"); $templ->assign_from_vars($scope, ...); decent enough? -Sterling /* {{{ proto array get_defined_vars([string type]) Returns a two-dimensional associative array of all defined variable names */ ZEND_FUNCTION(get_defined_vars) {
        zval **what,
             *globals,
             *current,
             *tmp;
    int argc = ZEND_NUM_ARGS();
    if (argc < 0 || argc > 1 ||
        zend_get_parameters_ex(argc, &what) == FAILURE) {
        WRONG_PARAM_COUNT;
    }
    array_init(return_value);
        if (argc > 0) {
            if (!strcmp((*what)->value.str.val, "globals")) {
            zend_hash_copy(return_value->value.ht, &EG(symbol_table),
                               (copy_ctor_func_t)zval_add_ref, &tmp, sizeof(zval *));
            }
            if (!strcmp((*what)->value.str.val, "current")) {
                zend_hash_copy(return_value->value.ht, EG(active_symbol_table),
                              (copy_ctor_func_t)zval_add_ref, &tmp, sizeof(zval *));
            }
    } else {
        MAKE_STD_ZVAL(globals);
        array_init(globals);
        zend_hash_copy(globals->value.ht, &EG(symbol_table),
                           (copy_ctor_func_t)zval_add_ref, &tmp, sizeof(zval *));
            MAKE_STD_ZVAL(current);
        array_init(current);
        zend_hash_copy(current->value.ht, EG(active_symbol_table),
                           (copy_ctor_func_t)zval_add_ref, &tmp, sizeof(zval *));
        if (zend_hash_add(return_value->value.ht, "globals", sizeof("globals"), (void **)&globals, sizeof(zval *), NULL) == FAILURE) {
                    zend_error(E_WARNING, "Cannot add gobal variables to return value from get_defined_vars()");
                RETURN_FALSE;
        }
            if (zend_hash_add(return_value->value.ht, "current", sizeof("current"), (void **)&current, sizeof(zval *), NULL) == FAILURE) {
                    zend_error(E_WARNING, "Cannot add current variables to return value from get_defined_vars()");
                    RETURN_FALSE;
            }
    }
} /* }}} */

« previous php.dev (#38700) next »