Bruno B B Magalhães wrote:
Hi guys..

well I have a little "problem", I succeeded on retrieving a value by it's key, but I want a clean and faster method... Let me explain:

I have the following function which I use to set variables in my framework like (configs, requests, etc)...


/***********************************************************************
* @function_name set_vars
* @function_type Method
* @function_input None
* @function_description None
***********************************************************************/
function set_var($key = '', $value = null)
{
    if(is_array($key))
    {
        if(count($key))
        {
            foreach($key as $key_key => $key_value)
            {
                $this->vars[$key_key] = $key_value;
            }
        }
    }
    elseif(is_array($value))
    {
        if(count($value))
        {
            foreach($value as $value_key => $value_value)
            {
                $this->vars[$key][$value_key] = $value_value;
            }
        }
    }
    else
    {
        $this->vars[$key] = $value;
    }
}

For example I have:

$this->vars['config']['database_type'] = 'mysql'; or...
$this->vars['http']['get'] = 'b';

But now I want a function that gets those values and subvalues, but there is one small catch, I need to dynamically get parsed arguments and check if key exists and returns it... I tried func_num_args, and func_get_args, without success.


I don't really have a clue as to what you want to do - but I took a guess.
maybe the following is along the lines of what you are looking to do?

the idea is that if you had a VarCollection object with the following:

        $obj->vars['a']['b']['c']['d']['e'] = 'ABCDE';

you could output the value 'ABCDE' by doing this:

        echo $obj->get_var('a','b','c','d','e');

the example below is not tested (mostly because I don't know
what I should have tested exactly but also because I have sloth
tendencies ;-)

class VarCollection
{
        var $vars;
        
        function VarCollection()
        {
                $this->vars = array();
        }

        function set_var($key = '', $value = null) { /* bla */ }
        function get_var()
        {
                $args = func_get_args();
                if (empty($args)) {
                        return null;
                }

                $varRef =& $this->vars;
                while ($arg = array_shift($args)) {
                        if (!isset($varRef[ $arg ])) {
                                return null;
                        } else {
                                $varRef =& $varRef[ $arg ];
                        }
                }

                return ($xyz = $varRef); // xyz because I'm paranoid about 
references
        }
}


Any ideas?

once, it involved Britney Spears, a 7.5 ton truck and 1000
gallons of ... oh wait you meant something else.

;-)



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to