1. Go to this page and download the library: Download macfja/value-provider 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/ */
macfja / value-provider example snippets
class Person {
public $firstName = '';
public $lastName = '';
}
class Person {
private $_firstName = '';
private $_lastName = '';
private $_known = true;
public function getFirstName() {
return $this->_firstName;
}
public function setFirstName($value) {
$this->_firstName = $value;
}
public function getLastName() {
return $this->_lastName;
}
public function setLastName($value) {
$this->_firstName = $value;
}
public function isKnown() {
return $this->_known;
}
public function setKnown($flag) {
$this->_known = $flag;
}
}
$jdoe = new Person();
$provider = new MutatorProvider();
// ...
$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);
// ...
echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"
class Person {
private $_firstName = 'John';
private $_lastName = 'Doe';
private $_known = true;
function __call($name, $arguments) {
switch ($name) {
case 'getFirstName':
return $this->_firstName;
case 'getLastName':
return $this->_lastName;
case 'isKnown':
return $this->_known;
case 'setFirstName':
$this->_firstName = $argument[0];
return;
case 'setLastName':
$this->_lastName = $argument[0];
return;
case 'setKnown':
$this->_known = $argument[0];
return;
}
throw new \BadFunctionCallException;
}
}
$jdoe = new Person();
$provider = new MutatorProvider();
// ...
$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);
// ...
echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"