PHP Classes

PHP Object Copy Properties: Copy values of an object to another

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 54 This week: 1All time: 10,567 This week: 560Up
Version License PHP version Categories
php-object-copy 1.0GNU General Publi...5PHP 5, Data types
Description 

Author

The package can copy values of an object to another.

It can take as parameters two given objects and uses reflection to discover their class variables, so it can copy those class variable values from the source object to the destination object.

The class can use getter functions to get the source object class variable values and setter functions to set the destination object class variable values.

It may also optionally exclude from the copy process class variables that are private, or static or protected.

Picture of Juraj Puchký
  Performance   Level  
Name: Juraj Puchký is available for providing paid consulting. Contact Juraj Puchký .
Classes: 17 packages by
Country: Czech Republic Czech Republic
Age: 41
All time rank: 109511 in Czech Republic Czech Republic
Week rank: 52 Up1 in Czech Republic Czech Republic Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php

include_once __DIR__ . '/../src/ObjectCopier.php';


use
BABA\Utils\ObjectCopier;

/**
 * Class A
 */
class A
{
    private
$private;
    public
$public;
    protected
$protected;

   
/**
     * A constructor.
     * @param $private
     * @param $public
     * @param $protected
     */
   
public function __construct($private, $public, $protected)
    {
       
$this->private = $private;
       
$this->public = $public;
       
$this->protected = $protected;
    }

   
/**
     * @return mixed
     */
   
public function getPrivate()
    {
        return
$this->private;
    }

   
/**
     * @param mixed $private
     */
   
public function setPrivate($private)
    {
       
$this->private = $private;
    }

   
/**
     * @return mixed
     */
   
public function getPublic()
    {
        return
$this->public;
    }

   
/**
     * @param mixed $public
     */
   
public function setPublic($public)
    {
       
$this->public = $public;
    }

   
/**
     * @return mixed
     */
   
public function getProtected()
    {
        return
$this->protected;
    }

   
/**
     * @param mixed $protected
     */
   
public function setProtected($protected)
    {
       
$this->protected = $protected;
    }

}

$a = new A('private', 'public', 'protected');
$b = new A('', '', '');

ObjectCopier::copyProperties($a, $b);

var_dump($a);
var_dump($b);

$a = new A('private', 'public', 'protected');
$b = new A('', '', '');

ObjectCopier::copyProperties($a, $b, array('private'));

var_dump($a);
var_dump($b);

$a = new A('private', 'public', 'protected');
$b = new A('', '', '');

ObjectCopier::copyPropertiesMap($a, $b, array('private' => 'public'));

var_dump($a);
var_dump($b);


Details

php-object-copy

Simple implementation of copy properties from one object to another by Reflection API.


  Files folder image Files  
File Role Description
Files folder imageexamples (1 file)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file simple-copy.php Example Example script

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:1
All time:10,567
This week:560Up