PHP code example of phpgt / propfunc
1. Go to this page and download the library: Download phpgt/propfunc 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/ */
phpgt / propfunc example snippets
use Gt\PropFunc\MagicProp;
/**
* @property-read bool $future True if the day is in the future
* @property-read int $daysApart Days between now and this day
*/
class Day {
use MagicProp;
public function __construct(
private DateTimeInterface $dateTime
) {}
// Expose the "dateTime" private property with read-only access:
private function __prop_get_dateTime():DateTimeInterface {
return $this->dateTime;
}
// Expose the "future" calculated property with read-only access:
private function __prop_get_future():bool {
$now = new DateTime();
return $now < $this->dateTime;
}
// Expose the "daysApart" calculated property with read-only access:
private function __prop_get_daysApart():int {
$now = new DateTime();
$diff = $now->diff($this->dateTime);
return $diff->days;
}
}
$day = new Day($dateTime);
echo "Day is $day->diff days in the ";
echo $day->future ? "future" : "past";
echo PHP_EOL;
$day->diff = 10;
echo "Exception thrown on line above!";