Ratings | | Unique User Downloads | | Download Rankings |
Not yet rated by the users | | Total: 95 | | All time: 9,773 This week: 66 |
|
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
 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 |
| |
 |
|
Innovation award
 Nominee: 5x |
|
Details
CopyObjectAttributesValues

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.
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.