PHP code example of deoliveiralucas / copy-object-attributes-values

1. Go to this page and download the library: Download deoliveiralucas/copy-object-attributes-values library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deoliveiralucas / copy-object-attributes-values example snippets


use CopyObjectAttributesValues\CopyObjectAttributesValues;

class ObjectA {
    private $attributeA = 'ObjectA_AttrA';
    private $attributeB = 'ObjectA_AttrB';
}
class ObjectB {
    private $attributeA = 'ObjectB_AttrA';
    private $attributeB = 'ObjectB_AttrB';
    private $attributeC = 'ObjectB_AttrC';
}

$objectA = new ObjectA();
$objectB = new ObjectB();

CopyObjectAttributesValues::from($objectA)->to($objectB);

var_dump($objectB);

/*
Output:
class ObjectB#2 (3) {
  private $attributeA =>
  string(13) "ObjectA_AttrA"
  private $attributeB =>
  string(13) "ObjectA_AttrB"
  private $attributeC =>
  string(13) "ObjectB_AttrC"
}
*/