PHP code example of emeraldinspirations / lib-objectdesignpattern-immutable
1. Go to this page and download the library: Download emeraldinspirations/lib-objectdesignpattern-immutable library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
emeraldinspirations / lib-objectdesignpattern-immutable example snippets
use emeraldinspirations\library\objectDesignPattern\immutable\ImmutableTrait;
class ExampleImmutableObject
{
use ImmutableTrait;
protected $Property;
/**
* Return value of Property
*
* @return \stdclass
*/
public function getProperty() : \stdclass
{
return $this->Property;
}
/**
* Create new instance with new Property value
*
* @param \stdclass $Property New value
*
* @return self
*/
public function withProperty(\stdclass $Property) : self {
return $this->with(__FUNCTION__, $Property);
}
/**
* Clone all specified properties are cloned as necessary
*
* @return void
*/
public function __clone()
{
// Create an array of references to properties to clone
$PropertiesToByCloned = [
&$this->Property,
// ^ - IMPORTANT: Be sure to pass by reference
];
self::cloneArrayRecursively($PropertiesToByCloned);
}
}