PHP code example of dooaki / class-accessor

1. Go to this page and download the library: Download dooaki/class-accessor 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/ */

    

dooaki / class-accessor example snippets



use dooaki\ClassAccessor\Accessor;

class Person
{

    private $first_name;
    private $age;

    use Accessor {
        _helperStringOrNullGetter as public getFirstName;
        _helperStringOrNullSetter as public setFirstName;
        _helperIntGetter as public getAge;
        _helperIntSetter as public setAge;
    }

    public function show()
    {
        echo "{$this->first_name} ({$this->age})";
    }
}

$p = new Person();
$p->setFirstName('john');
$p->setAge(23);

var_dump($p->getFirstName()); // string(4) "john"
var_dump($p->getAge()); // int(23)
$p->show(); // john (23)


$p->setFirstName(null); // ok name is nullable
$p->setAge("23"); // TypeError exception!



_DIR__ . '/DateAccessor.php';

use My\DateAccessor;
use dooaki\ClassAccessor\Accessor;

class DelayedDateTime {

    /** @var DateTimeInterface  */
    private $date_time;

    /** @var  DateInterval */
    private $delay;

    use Accessor, DateAccessor {
        _helperDateTimeInterfaceObjectGetter as public getDateTime;
        _helperDateTimeInterfaceObjectSetter as public setDateTime;
        _helperDateIntervalObjectOrNullGetter as public getDelay;
        _helperDateIntervalObjectOrNullSetter as public setDelay;
    }

    public function __construct(DateTime $date_time)
    {
        $this->date_time = $date_time;
    }

    public function get()
    {
        $retval = clone $this->date_time;
        if ($this->delay) {
            $retval->sub($this->delay);
        }
        return $retval;
    }
}

$m = new DelayedDateTime(new DateTime());
$m->setDelay(new DateInterval('P1D'));

echo $m->get()->format('Y-m-d H:i:s');