CodeIgniter Forums
Objectify - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Objectify (/showthread.php?tid=13562)

Pages: 1 2


Objectify - El Forum - 11-27-2008

[eluser]Jamie Rumbelow[/eluser]
I've written a simple function that turns an array into an object. It works recursivly, so this:

Code:
Array {

  [0]        => "Jamie",
  ['hello']  => "World",
  ['hi']     => Array { [0] => "Another Array" }

}

Becomes this:

Code:
Object {

  [0]       = "Jamie",
  ['hello'] = "World",
  ['hi']    = Object { [0] = "Another Array" }

}

It's pretty simple!

The function is here:

Code:
public function objectify($array)
    {
        (object)$object = "";
        
        foreach( $array as $key => $value )
        {
            if ( ! is_array( $value ) )
            {
                $object->$key = $value;
            }
            else
            {
                $object->$key = $this->objectify($value);
            }
        }
        
        return $object;
    }

Cheers!


Objectify - El Forum - 11-27-2008

[eluser]mihailt[/eluser]
hmm... consider this for improving your function
Code:
$obj = (object) array('foo' => 'bar', 'property' => 'value');



Objectify - El Forum - 11-27-2008

[eluser]xwero[/eluser]
mihailt you are right the function is only useful if you need a nested array to be fully transformed to objects.


Objectify - El Forum - 11-27-2008

[eluser]mihailt[/eluser]
what i meant was that

Code:
(object)$object = "";

is not a proper way to do it.
so option one: you can you the construction i gave you above in that function of yours,
and option two: you can make new stdClass instance.


Objectify - El Forum - 11-27-2008

[eluser]xwero[/eluser]
I'm wondering if it's really that wrong. What you say is that typecasting is worst than creating a new stdClass instance? Could you tell me why that is?

I would do it like your option 2 because if you want the function to be php4 compatible you can't typecast because the foreach in php4 can't iterate an object.


Objectify - El Forum - 11-27-2008

[eluser]mihailt[/eluser]
why wrong: because, you create an inderect string and then convert it to an object, insted of creating object in first place.

as for php4 support - my opinion is - don't use dead languages. i mean it's officialy dead,
and is't not long for php6 to come out, and we are still considering the use of php4 ? any reason why?


Objectify - El Forum - 11-27-2008

[eluser]xwero[/eluser]
I'm not going to convert all the sites i made in php4 to php5 or 6 just to have access to one function. if i would do that i would have no personal life for a very long time. They have to throw a whole lot of money my way for that to happen.


Objectify - El Forum - 11-27-2008

[eluser]mihailt[/eluser]
i think you would be surprised how easy they would convert to php5( with php6 it's a different story), and the reason would be not "to have to access to one function", but to be able to use modern methodologies, technologies and features in your legacy applications that would improve your quality of work.

It's your environment, so it's you who would be able or not to use something.

Ahh,.. but enough of this , as many people as many opinions, so let's not holy war about it.
Regards


Objectify - El Forum - 11-27-2008

[eluser]xwero[/eluser]
You are right most php4 site i coded will be compatible with php5 but moving applications from a php4 to a php5 running server still requires to do all the tests to make sure nothing breaks for some reason. Basically this is the economics vs innovation debate that will stay alive as long as software development will be around.

I think as developers we see eye to eye but in a world with a lot of people that aren't developers it's not that clear cut.


Objectify - El Forum - 11-27-2008

[eluser]GSV Sleeper Service[/eluser]
interesting idea. I can't see when I'd ever use it, and wouldn't $object be obliterated on each recursive call?