PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 96 This week: 1All time: 9,834 This week: 560Up
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
Number 4


Prize: 1 Year Subscription to NomadPHP Advanced PHP Learning
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
Picture of Lucas de Oliveira
  Performance   Level  
Name: Lucas de Oliveira <contact>
Classes: 5 packages by
Country: Brazil Brazil
Age: 29
All time rank: 2755192 in Brazil Brazil
Week rank: 411 Up35 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 5x

Documentation

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:96
This week:1
All time:9,834
This week:560Up