PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 96 All time: 9,858 This week: 660Up
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: 30
All time rank: 2758193 in Brazil Brazil
Week rank: 216 Up20 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 (14)  
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 (14)  /  src  
File Role Description
  Plain text file CopyObjectAttributesValues.php Class Class source

  Files folder image Files (14)  /  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 (14)  /  test  /  unit  
File Role Description
Files folder imageCopyObjectAttributesValuesTest (2 files)
  Plain text file CopyObjectAttributesValuesTest.php Class Class source

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:0
All time:9,858
This week:660Up