Re: casting arrays as objects

From: Date: Fri, 13 Apr 2001 21:41:15 +0000
Subject: Re: casting arrays as objects
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-48489@lists.php.net to get a copy of this message
why mot just <?php
        $foo = new Foo( $db->fetchRow(DB_FETCHMODE_ASSOC) );
?> If you have to leave this part of the project for any length of time, coming back to the above would be much less confusing. ( i think so anyway :) morgan At 03:12 PM 4/13/2001 -0500, you wrote:
Can you cast an array to an object of your choosing? If so, will the keys of the array match up with the attributes of your object? Say I have a class like this: class Foo {
    var $foo;
    var $bar;
    function Foo($foo = '', $bar = '') {
    ...
    }
    ...
} Then I have an array like:
    $array['foo'] = "Hello";
    $array['bar'] = "world";
If I do this: $foo = new Foo(); $foo = (object)$array; I've seen something similar done, but how do you keep $foo from forgetting which class it belongs to? The second assignment is independent of the first one, so $foo should become a plain object and forget that it's a "Foo" object. Any ideas? By the way, I was thinking this would be a really nifty way to fetch rows from the database into a pre-written class instead of doing it all manually. You'd have all the object properties automatically set by: $my_object = (object)$db->fetchRow(DB_FETCHMODE_ASSOC); I'm just worried about the object losing its membership in its original class. Dean. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
=== Morgan Curley Partner, e4media 212 674 7698 mcurley@e4media.com http://www.e4media.com __________________________________________________

« previous php.general (#48489) next »