Part of the EllisLab Network
   
1 of 2
1
Objectify
Posted: 27 November 2008 03:35 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  739
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 

Sparkplugs - Intuitive add-ons for ExpressionEngine and MojoMotor
—-
Taggable - ExpressionEngine Tagging Module
MojoBlog 2 - MojoMotor Blog Module/Add-on
—-
Freelance Web Developer - @jamierumbelow - http://jamieonsoftware.com

Profile
 
 
Posted: 27 November 2008 05: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 06:02 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
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 06: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 06:50 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
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 07: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 07:12 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
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 11: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 12:54 PM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
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 01:05 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  535
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 01:32 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  739
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 

Sparkplugs - Intuitive add-ons for ExpressionEngine and MojoMotor
—-
Taggable - ExpressionEngine Tagging Module
MojoBlog 2 - MojoMotor Blog Module/Add-on
—-
Freelance Web Developer - @jamierumbelow - http://jamieonsoftware.com

Profile
 
 
   
1 of 2
1