1. Go to this page and download the library: Download dkd/php-populate 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/ */
dkd / php-populate example snippets
namespace MyNamespace;
use Dkd\Populate\PopulateTrait;
use Dkd\Populate\PopulateInterface;
/**
* My populatable class
*/
class MyClassWhichUsesPopulateTrait implements PopulateInterface
{
use PopulateTrait;
/**
* @var string
*/
protected $name;
/**
* @var boolean
*/
protected $before = true;
/**
* @var boolean
*/
protected $after = false;
/**
* @param string $name
*/
public function setName($name) {
$this->name = $name
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param boolean $before
*/
public function setIsBefore($before) {
$this->before = $before;
}
/**
* @return boolean
*/
public function isBefore() {
return $this->before;
}
/**
* @param boolean $after
*/
public function setIsAfter($after) {
$this->after = $after;
}
/**
* @return boolean
*/
public function isAfter() {
return $this->after;
}
}
$source = someFakeMethodWhichRetrievesAnObjectImplementingPopulate();
$copy = new MyClassWhichUsesPopulateTrait();
// copy all properties:
$copy->populate($source);
// copy properties from one property name to another:
$copy->populate($source, array('before' => 'after'));
// ...$copy's "after" property now contains $source's "before" property value
// copy only a few properties:
$copy->populate($source, array('before'), TRUE);
// ...$copy was only populated with the value of $source's "before" property.
$source = someFakeMethodWhichRetrievesAnObjectImplementingPopulate();
// export all properties:
$array = $source->exportGettableProperties();
// export all properties but export "before" value as "after" key in array:
$array = $source->exportGettableProperties(array('before' => 'after'));
// export only some properties and map their names to other names:
$array = $source->exportGettableProperties(array('before' => 'after'), TRUE);
// export only some properties but keep their names:
$array = $source->exportGettableProperties(array('before'), TRUE);
$copy->populateWithClones($source);
// Solution #1: populate everything with references, then overwrite
// those properties that lag set to `true`:
$copy->populate($source);
$copy->populateWithClones($source, array('cloneProperty1', 'cloneProperty2'), true);
// Solution #2: the reverse of the above; populate everything with
// clones then overwrite those properties requiring references:
$copy->populateWithClones($source);
$copy->populate($source, array('referenceProperty1', 'referenceProperty2'), true);
try {
$populatable = new MyClassWhichUsesPopulateTrait();
$populatable->populate($valuesWithPotentialErrors);
} catch (\Dkd\Populate\AccessException $error) {
// attempt at illegal access - could be a security issue.
} catch (\Dkd\Populate\Exception $error) {
// general failure - do something about it.
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.