Jump to content

Combining two objects


haku

Recommended Posts

Hi,

 

are these 2 objects of the same class? if not, then the fact that they are 2 different object means that they are supposed to be seperate and they belong to 2 different entities.

 

Sorry if i got it wrong, but could you please elaborate more on why u want to merge 2 objects?

 

Thanks

Samshel

Link to comment
Share on other sites

They are not part of a class.

 

Object 1 contains:

 

$obj1->filename

$obj1->path

 

Object 2 contains:

 

$obj2->uploaded_by

$obj2->uploaded_on

 

I want to combine them because the data goes together, and should all be part of one object, not two.

Link to comment
Share on other sites

Ideally if you want the data to be together, you should have a parent class which has all these member functions..but to answer your question..

you can convert all properties of the 2 objects into arrays first and then use array_merge

 

$arr1 = get_object_vars($obj1);
$arr2 = get_object_vars($obj2);
$arrFinal = array_merge($arr1, $arr2);

Link to comment
Share on other sites

Ideally that would be the case. But it's a drupal setup I was working with, and the original object doesn't have the properties of the second object. But the function only returns one object, so the properties of the second object needed to be added into the first object.

 

I actually just ended up doing it manually:

 

$obj1->uploaded_by = $obj2->uploaded_by;
$obj1->uploaded_on = $obj2->uploaded_on;
return $obj1;

 

Not the cleanest method, but it worked fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.