1. Go to this page and download the library: Download icanboogie/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/ */
icanboogie / accessor example snippets
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property-read mixed $property
*/
class ReadOnlyProperty
{
use AccessorTrait;
protected function get_property()
{
return 'value';
}
}
$a = new ReadOnlyProperty;
echo $a->property; // value
$a->property = null; // throws ICanBoogie\PropertyNotWritable
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property-read mixed $property
*/
class ReadOnlyProperty
{
use AccessorTrait;
private $property = "value";
protected function get_property()
{
return $this->property;
}
}
$a = new ReadOnlyProperty;
echo $a->property; // value
$a->property = null; // throws ICanBoogie\PropertyNotWritable
use ICanBoogie\Accessor\AccessorTrait;
class Connection
{
// …
}
/**
* @property-read Connection $connection
*/
class Model
{
use AccessorTrait;
/**
* @var Connection
*/
private $connection;
protected function get_connection(): Connection
{
return $this->connection;
}
protected $options;
public function __construct(Connection $connection, array $options)
{
$this->connection = $connection;
$this->options = $options;
}
}
$connection = new Connection(…);
$model = new Model($connection, …);
$connection === $model->connection; // true
$model->connection = null; // throws ICanBoogie\PropertyNotWritable
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property-write mixed $property
*/
class WriteOnlyProperty
{
use AccessorTrait;
protected function set_property($value)
{
// …
}
}
$a = new WriteOnlyProperty;
$a->property = 'value';
echo $a->property; // throws ICanBoogie\PropertyNotReadable
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property-write mixed $property
*/
class WriteOnlyProperty
{
use AccessorTrait;
private $property = 'value';
protected function set_property($value)
{
$this->property = $value;
}
}
$a = new WriteOnlyProperty;
$a->property = 'value';
echo $a->property; // throws ICanBoogie\PropertyNotReadable
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property int $minutes
*/
class Time
{
use AccessorTrait;
public $seconds;
protected function set_minutes(int $minutes)
{
$this->seconds = $minutes * 60;
}
protected function get_minutes(): int
{
return $this->seconds / 60;
}
}
$time = new Time;
$time->seconds = 120;
echo $time->minutes; // 2
$time->minutes = 4;
echo $time->seconds; // 240
use ICanBoogie\Accessor\AccessorTrait;
class Article
{
use AccessorTrait;
public $title;
public $slug;
public function __construct(string $title, string $slug = null)
{
$this->title = $title;
if ($slug)
{
$this->slug = $slug;
}
else
{
unset($this->slug);
}
}
protected function get_slug(): string
{
return \ICanBoogie\normalize($this->title);
}
}
$article = new Article("This is my article");
echo $article->slug; // this-is-my-article
$article->slug = "my-article";
echo $article->slug; // my-article
unset($article->slug);
echo $article->slug; // this-is-my-article
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\DateTime;
/**
* @property DateTime $created_at
*/
class Article
{
use AccessorTrait;
private $created_at;
protected function set_created_at($datetime)
{
$this->created_at = $datetime;
}
protected function get_created_at(): DateTime
{
$datetime = $this->created_at;
if ($datetime instanceof DateTime)
{
return $datetime;
}
return $this->created_at = ($datetime === null) ? DateTime::none() : new DateTime($datetime, 'utc');
}
}
use ICanBoogie\Accessor\AccessorTrait;
/**
* @property-read string $property
*/
class Plain
{
use AccessorTrait;
protected function get_property()
{
return "value";
}
}
class Awesome extends Plain
{
protected function get_property()
{
return "awesome " . parent::get_property();
}
}
$plain = new Plain;
echo $plain->property; // value
$awesome = new Awesome;
echo $awesome->property; // awesome value
use ICanBoogie\Accessor\AccessorCamelTrait;
/**
* @property-read $camelProperty
*/
class CamelExample
{
use AccessorCamelTrait;
private $camelProperty;
protected function getCamelProperty()
{
return $this->camelProperty;
}
public function __construct($value)
{
$this->camelProperty = $value;
}
}
$a = new CamelExample("value");
echo $a->camelProperty; // value
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.