PHP Classes

File: examples/simple-copy.php

Recommend this page to a friend!
  Classes of Juraj Puchký   PHP Object Copy Properties   examples/simple-copy.php   Download  
File: examples/simple-copy.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Object Copy Properties
Copy values of an object to another
Author: By
Last change: Update of examples/simple-copy.php
Date: 3 years ago
Size: 1,699 bytes
 

Contents

Class file image Download
<?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);