承接 authanram/attributes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

authanram/attributes

Composer 安装命令:

composer require authanram/attributes

包简介

Low level utilization of PHP8 attributes

README 文档

README

Simple usage of attributes brought by PHP8.

Install

composer require authanram/attributes

Usage

Create an attribute class for any purpose, casting, mapping, etc.

<?php

namespace AwesomeProject\Attributes;

#[Attribute(Attribute::TARGET_PROPERTY)]
AwesomeAttribute
{
    /**
     * @param mixed ...$args // ['whatever', 'data']
     */
    public funtion __construct(mixed ...$args)
    {}
    
    /**
     * @param object $context   // Instance of AwesomeProject\AwesomeClass
     * @param string $name      // 'foo' (The name of the property the attribute belongs to)
     * @param mixed ...$args    // ['your', 'args']
     * 
     * @return void
     */
    public function handle(object $context, string $name, mixed ...$args)
    {
    }
    
    /**
     * @param mixed $old // 'qux'
     * 
     * @return string
     */
    public function value(mixed $old): string
    {
        return 'bar';
    }
}

Use your Attribute (somewhere)

For e.g.

<?php

namespace AwesomeProject;

use Authanram\Attributes;

AwesomeClass
{
    #[Attributes\AwesomeAttribute('whatever', 'data')]
    public string $foo = 'qux';
    
    public function __construct()
    {
        new Attributes($this, 'your', 'args');
    }
}

Create an Instance

$instance = new \AwesomeProject\AwesomeClass();

$instance->foo; // 'bar'

Some Words

The methods handle and value will only be called if present. Means if the constructor (and arguments) plus of one these methods will fit to your needs, the other one can be omitted.

Under the hood an instance of the attribute class will be created and the methods handle and value will be called, if present.

The return value of the method value will be assigned the corresponding property. Through the arguments $context and $name, passed to the method handle, you'll be able to assign a value manually to the property the attribute belongs to, e.g. $context->{$name} = 'qux';

In general, this should equip you with some low level tools to utilize attributes in your code.

Testing

vendor/bin/pest

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固