Part of the EllisLab Network
   
1 of 2
1
Objectify
Posted: 27 November 2008 02:35 AM   [ Ignore ]  
Moderator
Avatar
RankRankRank
Total Posts:  603
Joined  02-24-2008

I’ve written a simple function that turns an array into an object. It works recursivly, so this:

Array {

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

}

Becomes this:

Object {

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

}

It’s pretty simple!

The function is here:

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!

 Signature 

Jamie Rumbelow freelance developer / writer / speaker
+44 (0)7956 363875 jamie(at)jamierumbelow(dot)net
http://jamierumbelow.net

Profile
 
 
Posted: 27 November 2008 04:40 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  61
Joined  10-22-2007

hmm… consider this for improving your function

$obj = (object) array('foo' => 'bar', 'property' => 'value');
 Signature 

CLE - Controller Loader Extension

Profile
 
 
Posted: 27 November 2008 05:02 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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

Profile
 
 
Posted: 27 November 2008 05:16 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  61
Joined  10-22-2007

what i meant was that

(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.

 Signature 

CLE - Controller Loader Extension

Profile
 
 
Posted: 27 November 2008 05:50 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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.

Profile
 
 
Posted: 27 November 2008 06:01 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  61
Joined  10-22-2007

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?

 Signature 

CLE - Controller Loader Extension

Profile
 
 
Posted: 27 November 2008 06:12 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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.

Profile
 
 
Posted: 27 November 2008 10:12 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  61
Joined  10-22-2007

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

 Signature 

CLE - Controller Loader Extension

Profile
 
 
Posted: 27 November 2008 11:54 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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.

Profile
 
 
Posted: 27 November 2008 12:05 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  526
Joined  03-13-2008

interesting idea. I can’t see when I’d ever use it, and wouldn’t $object be obliterated on each recursive call?

 Signature 

:wq

Profile
 
 
Posted: 27 November 2008 12:32 PM   [ Ignore ]   [ # 10 ]  
Moderator
Avatar
RankRankRank
Total Posts:  603
Joined  02-24-2008

@GSV - No, each $object variable is in it’s own vaiable scope inside the function so it wouldn’t get erased.

And I’ve been using it to convert the $_POST array - it’s just nice to have it as an object.

 Signature 

Jamie Rumbelow freelance developer / writer / speaker
+44 (0)7956 363875 jamie(at)jamierumbelow(dot)net
http://jamierumbelow.net

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 119762 Total Logged-in Users: 34
Total Topics: 125937 Total Anonymous Users: 3
Total Replies: 662565 Total Guests: 351
Total Posts: 788502    
Members ( View Memberlist )
Newest Members:  frostiexadamsmithdj1White HattkatimariedelcaljeffewensJuan AlumadontheobaldBCFEA2FBS