Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add two objects into one using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 251
    Comment on it

    Hello Reader's if you want to add two or more objects into one. Then you can see it into the example below:-

    Ideally if you want the data to be together, you should have a parent class which has all these member functions and you should have a parent class with all common properties for every child, and each child has its own properties declared in it.

       $obj1 = new stdClass();
        $obj1->a = 'obj1:a';
        $obj1->b = 'obj1:b';
        $obj1->c = 'obj1:c';
    
        $obj2 = new stdClass();
        $obj2->a = 'obj2:a';
        $obj2->b = 'obj2:b';
        $obj2->d = 'obj2:d';
    
        $obj3 = new Compositor();
        $obj3->merge($obj1, $obj2);
        $obj1->c = '#obj1:c';
        var_dump($obj3->a, $obj3->b, $obj3->c, $obj3->d);
        // obj2:a, obj2:b, obj1:c, obj2:d
        $obj1->c;
    
        $obj3 = new Compositor(TRUE);
        $obj3->merge($obj1)->with($obj2);
        $obj1->c = '#obj1:c';
        var_dump($obj3->a, $obj3->b, $obj3->c, $obj3->d);
        // obj1:a, obj1:b, obj1:c, obj2:d
        $obj1->c = 'obj1:c';
    
        $obj3 = new Compositor(FALSE, TRUE);
        $obj3->with($obj1)->with($obj2);
        $obj1->c = '#obj1:c';
        var_dump($obj3->a, $obj3->b, $obj3->c, $obj3->d);
        // obj1:a, obj1:b, #obj1:c, obj2:d
        $obj1->c = 'obj1:c';
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: