On 2/6/2009 20:56, Brad Griffith wrote:
If we're just talking about an stdClass, can't you do:

$array = (array) $myStdClass;

And in the other direction:

$object = (object) $myArray;


I agree with Brad on this one. I often find myself doing something like this:

$some_var = (object)array(
        'property_1' => 'value_1',
        'property_2' => 'value_2',
);


On Fri, Feb 6, 2009 at 11:46 AM, Ralph Schindler
<[email protected] <mailto:[email protected]>> wrote:

    Another option:

    $items = array(); // this could be any set of data or null
    $oTrack = new ArrayObject($items, ArrayObject::ARRAY_AS_PROPS);

    That would give you access to this kind of functionality:
    http://www.php.net/~helly/php/ext/spl/classArrayObject.html
    <http://www.php.net/%7Ehelly/php/ext/spl/classArrayObject.html>

    -ralph




    On 2/6/09 11:43 AM, "Michael Tramontano"
    <[email protected]
    <mailto:[email protected]>> wrote:

        What you have there works fine, is simple, and is relatively
        quick so I'm not sure why you're looking for something built-in.

        That being said, an alternative is to make a class that simply
        copies the array to a private variable, and uses the magic __get
        and __set functions for access outside of the class. Perhaps
        something like this might help if you want to go in that
        direction:
        
http://us2.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

        Mike Tramontano
        *eFashion Solutions -* Sr Backend Developer
        [email protected]
        <mailto:[email protected]>


        *From:* [email protected] <mailto:[email protected]>
        [mailto:[email protected]] <mailto:[email protected]%5d> *On
        Behalf Of *A.J. Brown
        *Sent:* Friday, February 06, 2009 12:24 PM
        *To:* Zend Framework General
        *Subject:* [fw-general] stdClass Question

        Hey guys,

        This is probably more approriate for the PHP list, but I'm not
        subscribed with this email address, so I'll pose it here.

        Is there a better / built in way to turn an array into a stdClass?

        //TODO (is there / why isn't there) a built in way to do this?
        $oTrack = new stdClass();
        foreach( $trackData as $key => $value ) {
        $oTrack->$key = $value;
        }


        I'm doing this because the user has the option of passing in
        either a model (an object type), or an array of the data that
        would be in the model. But, I don't want to have two different
        code paths for processing the data.


    --
    Ralph Schindler
    Software Engineer | [email protected]
    <mailto:[email protected]>
    Zend Framework | http://framework.zend.com/



--
Ionut G. Stan
I'm under construction  |  http://igstan.blogspot.com/

Reply via email to