f2/getset

一个名为 `F2\GetSet` 的特质,它实现了具有公共、受保护和私有可见性以及类型检查的获取器和设置器。

1.0.0 2020-01-12 22:31 UTC

This package is not auto-updated.

Last update: 2024-09-17 18:52:53 UTC


README

一个简单的库,允许使用PHP以一致的方式创建获取器和设置器。支持可见性和类型提示。

简单的获取器和设置器

<?php
class MyClass {
    use F2\GetSet;

    private $secret_value = "Initial value";

    /**
     * Any method that starts with "get_" is automatically a getter
     *
     * public, protected and private visibility is accepted and enforced.
     * Type checking is enforced by PHP automatically.
     */
    public function get_value(): string {
        return $this->secret_value;
    }

    /**
     * Any method that starts with "set_" is automatically a getter
     *
     * public, protected and private visibility is accepted and enforced.
     * Type checking is enforced by PHP automatically.
     */
    public function set_value(string $value): void {
        $this->secret_value = $value;
    }
}

$instance = new MyClass();

$instance->value .= ' and some more';
// Log: The property 'value' was read
// Log: The property 'value' was set to 'Initial value and some more'

## No dependencies

This library has no dependencies (except a development dependency f2/asserty)