Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:01:01+00:00 2026-05-30T07:01:01+00:00

How can I concatenate a class onto another class in this manner? class Person

  • 0

How can I concatenate a class onto another class in this manner?

class Person { }
$p1 = new Person();
$p2 = new Person();
$p3 = $p1 . $p2;

Keep in mind that I will always know everything in the class that needs to be concatenated ahead of time, so generalization is not an issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:01:03+00:00Added an answer on May 30, 2026 at 7:01 am

    This will concatenate two objects together assuming they’re the same type

    <?php
    
    function combine_objects($obj1, $obj2) {
      if (get_class($obj1) !== get_class($obj2)) {
        throw new Exception('Class mismatch');
      }
    
      $className = get_class($obj1);
      $newObject = new $className;
      foreach(get_object_vars($obj1) as $key => $value) {
        $newObject->$key = $obj1->$key . $obj2->$key;
      }
    
      return $newObject;
    }
    
    class Person {
      public $first;
      public $last;
      public $id;
    }
    
    $john = new Person;
    $john->first = 'John';
    $john->last = 'Smith';
    $john->id = 1;
    
    
    $mary = new Person;
    $mary->first = 'Mary';
    $mary->last = 'York';
    $mary->id = 5;
    
    $p3 = combine_objects($john, $mary);
    
    var_dump($p3);
    
    /*
    object(Person)#3 (3) {
      ["first"]=>
      string(8) "JohnMary"
      ["last"]=>
      string(9) "SmithYork"
      ["id"]=>
      string(2) "15"
    }
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i concatenate var names to declare new vars in javascript?: var foo
x = hello world.to_sym puts x.class This works and allows me to concatenate the
I have an Address class in C# that looks like this: public class Address
I'm trying to concatenate a class selector using multiple variables that come from user
I have a class that looks like this: public class MyModel{ public int TheId
Ok, I can't get this code work: I want to concatenate my custom manipulators.
In python, you can concatenate boolean values, and it would return an integer. Example:
How can I concatenate two char columns within a perform screen? example: table sample
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.