Welcome Guest, Not a member yet? Register   Sign In
is it possible to create a moblog with CI
#18

[eluser]sophistry[/eluser]
mostly i avoid variable variables because the result in baffling code (as you noted). also, it's a general rule of thumb of mine that if you think "i'll use variable variables" that really means that you probably need another class in the design and you need to handle the operation via properties.

that said, sometimes i'll use them in a pinch. and this little section of code you called out is kind of like a little stubby embryonic class. in this case, the code is looping over an array of objects building an array or arrays that needs the keys and they need to stay in sync and get filled with values from the array that is being looped. that's pretty much the only valid use for variable variables in my book.

i'll step back a bit to give this operation some context.

when a variable is not set and you try to use it, PHP issues a warning. you can suppress those warnings or make sure they don't happen by always having variables set when you try to use them.

this array (up a few lines from the foreach in question) lists all the variables we want to have set so that PHP doesn't complain:
Code:
$address_keys_we_need_to_be_set = explode(' ', 'from to cc bcc reply_to sender return_path');
those correspond to the header object returned by the imap_headerinfo() function in this:
Code:
$header_obj=imap_headerinfo($this->IMAP_resource, $this->msg_id);

so, this code checks whether the address keys are in the header object. it looks for those keys (from to cc bcc reply_to sender return_path).
if the key is in the object then an empty array is created in a new variable named for the key (this first use of variable variable).

Code:
$$key = array();

so, now we have a variable that holds an empty array. the variable is called $from (assuming this is the first iteration of the foreach)
then, another foreach starts because the header_obj has a nested array that holds more objects.
the code makes the object into an array and stacks it up onto $arr
the personal part of the address (if any) is decoded (this is a bit of a loose hack here since there could be other encodings)

then, finally (to your question), the array ($arr) that we just pulled out the the header object is pushed onto the array named $from (assuming first iteration or more generally $$key) so $from ends up with a nested array of arrays instead of a nested array of objects.

the whole thing is really just reorganizing the header_obj to make it easier to use. there is probably a slew of fat that could be trimmed from this beast! but, it works and gets you a nice tidy array of arrays instead of mixed objects and arrays. i don't like objects with no classes (the PHP std object) because i don't know how to manipulate the data inside them. i'd like to learn if there is a way to manipulate (get and set) std object properties, but right now i just turn them into arrays.

Code:
// turn each of the arrays of objects into arrays of arrays
    // with each address part getting encoding
    foreach ($address_keys_we_need_to_be_set as $key)
    {    
        // make sure the array item is set
        if (isset($header_obj->$key))
        {
            // it's there,
            // variable is named for the key
            $$key = array();
            $arr = array();
            foreach ($header_obj->$key as $obj)
            {
                // coerce it to an array
                $arr[] = (array)$obj;
                //p($key);
                //p($arr);
                // take the personal part and apply decoding
                if (isset($arr['personal']))
                {
                    $arr['personal'] = $this->decode_mime_text($arr['personal']);
                }
                
                // push the array onto the array
                array_push( $$key, $arr );
            }
            $email_arrays_array[$key] = $arr;
            //p($$key);
        }
        
    }


Messages In This Thread
is it possible to create a moblog with CI - by El Forum - 09-19-2007, 06:31 PM
is it possible to create a moblog with CI - by El Forum - 09-19-2007, 06:42 PM
is it possible to create a moblog with CI - by El Forum - 09-19-2007, 10:34 PM
is it possible to create a moblog with CI - by El Forum - 09-20-2007, 08:08 AM
is it possible to create a moblog with CI - by El Forum - 09-20-2007, 11:08 PM
is it possible to create a moblog with CI - by El Forum - 09-21-2007, 02:53 AM
is it possible to create a moblog with CI - by El Forum - 09-21-2007, 08:10 AM
is it possible to create a moblog with CI - by El Forum - 09-23-2007, 07:25 AM
is it possible to create a moblog with CI - by El Forum - 09-23-2007, 09:10 AM
is it possible to create a moblog with CI - by El Forum - 09-24-2007, 04:46 AM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 03:27 AM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 04:41 AM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 07:21 AM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 07:57 AM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 07:17 PM
is it possible to create a moblog with CI - by El Forum - 09-25-2007, 07:32 PM
is it possible to create a moblog with CI - by El Forum - 09-26-2007, 11:27 AM
is it possible to create a moblog with CI - by El Forum - 09-26-2007, 12:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB