定制 tourze/getter-setter-trait 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/getter-setter-trait

最新稳定版本:1.0.0

Composer 安装命令:

composer require tourze/getter-setter-trait

包简介

Getter/Setter Trait

README 文档

README

English | 中文

Latest Stable Version License PHP Version Coverage Status

A lightweight PHP library providing reusable Getter and Setter traits for object property access, inspired by Yii2 conventions. It enables magic property access (__get, __set) with robust exception handling for unknown or write/read-only properties.

Features

  • Simple integration of getter/setter magic methods via traits
  • Yii2-style exception handling for property access
  • Automatic detection of getter/setter methods
  • Support for property existence checks
  • Designed for PHP 8.1+

Installation

Requires PHP 8.1 or higher.

Install via Composer:

composer require tourze/getter-setter-trait

Quick Start

use Tourze\GetterSetterTrait\GetterTrait;
use Tourze\GetterSetterTrait\SetterTrait;
use Tourze\GetterSetterTrait\PropertyTrait;

class User
{
    use GetterTrait, SetterTrait, PropertyTrait;

    private string $name = '';

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

$user = new User();
$user->name = 'Tom';
echo $user->name; // Tom

Documentation

  • GetterTrait: Implements __get($name) for magic property reading.
  • SetterTrait: Implements __set($name, $value) for magic property writing.
  • PropertyTrait: Provides hasProperty, canGetProperty, canSetProperty helpers.
  • Throws UnknownPropertyException for undefined properties, InvalidCallException for read/write-only properties (compatible with Yii2).

Advanced Usage

  • You may override getter/setter methods for custom logic.
  • Use PropertyTrait to check property accessibility programmatically.

Contributing

Contributions are welcome! Please follow these steps:

  • Submit issues for bugs or feature requests.
  • Fork the repository and create pull requests.
  • Ensure code style and run tests before submitting.
  • Follow PSR-12 coding standards.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

tourze Team

This README is generated based on the actual codebase and follows the project documentation rules.

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-21