Welcome Guest, Not a member yet? Register   Sign In
easy way to cast stdClass object into instance of my own class?
#1

[eluser]sophistry[/eluser]
i have a stdClass object returned from the imap email extension function: imap_headerinfo()

i want to convert that stdClass object (or basically get its properties) into a message_header class that i wrote - it has methods i wrote that deal with the email header data.

here's how i am doing it now in a method (i'm not happy about this way, i found it on the comments for the php.net OO manual page)
Code:
function _set_class_vars( &$obj )
    {
        $class = get_class($this);
        $class_vars = get_class_vars($class);
        
        // check that each of the passed parameters are valid before setting the
        // appropriate class variable.
        foreach ( $obj as $var => $value ){
            if ( array_key_exists( $var, $class_vars ) ){        
                $this->$var = $value;
            }
            else{
                //p('setClassVars: class variable "'.$var.'" does not exist on class "' .$class.'"');
            }
        }
    }

is there another way to do this? like "type casting" from stdClass to custom class.

thanks in advance for any advice around this subject.




Theme © iAndrew 2016 - Forum software by © MyBB