PHP Classes
elePHPant
Icontem

PHP Copy Object Properties to Another: Copy the values of attributes to another object

Recommend this page to a friend!
  Info   View files Documentation   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-04-29 (25 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 46 This week: 3All time: 9,513 This week: 135Up
Version License PHP version Categories
copy-object-attribut 1.0.0MIT/X Consortium ...7Language, PHP 7
Description Author

This class can copy the values of attributes to another object.

It can take too given objects as parameters and uses reflection to determine what are the source object class variables.

The class can traverse the class variables list and copy the variable values from the source object to the destination object.

Innovation Award
PHP Programming Innovation award nominee
April 2019
Nominee
Vote
PHP provides means to create an object using another object as source by using the clone object feature.

However, when you just want to copy the values of an object to another existing object, you need to copy the object variable values one by one. This class implements that functionality.

Manuel Lemos
  Performance   Level  
Name: Lucas de Oliveira <contact>
Classes: 4 packages by
Country: Brazil Brazil
Age: 24
All time rank: 2877212 in Brazil Brazil
Week rank: 121 Up6 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 4x

Details

CopyObjectAttributesValues

Build Status Code Coverage Code Quality License MIT Packagist

Simple helper to copy attributes values with the same name from one object to an other.

Installation

composer require deoliveiralucas/copy-object-attributes-values

Usage

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"
}
*/

Contributing

Please see CONTRIBUTING for details.

License

CopyObjectAttributesValues is released under the MIT License. Please see License File for more information.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetest (1 file, 1 directory)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file CopyObjectAttributesValues.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imageunit (1 file, 1 directory)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  test  /  unit  
File Role Description
Files folder imageCopyObjectAttributesValuesTest (2 files)
  Plain text file CopyObjectAttributesValuesTest.php Class Class source

  Files folder image Files  /  test  /  unit  /  CopyObjectAttributesValuesTest  
File Role Description
  Plain text file ClassForAttributesATest.php Class Class source
  Plain text file ClassForAttributesBTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:46
This week:3
All time:9,513
This week:135Up