定制 jimbo2150/php-utilities 二次开发

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

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

jimbo2150/php-utilities

最新稳定版本:v1.0.0

Composer 安装命令:

composer require jimbo2150/php-utilities

包简介

Various utilities for PHP projects or libraries.

README 文档

README

Version License PHP Required Version

PHP Utilities

A collection of useful PHP utility functions, interfaces, classes, traits, and enums to simplify common tasks and enhance your PHP projects.

Table of Contents

Installation

This library is distributed via Composer's Packagist repository. To install it, run the following command in your project directory:

composer require jimbo2150/php-utilities

Usage

Traitable

The Traitable trait provides a hasTrait($traitName) method that checks if the given trait is associated with the object instance. It works similar to how instanceof works with class hierarchy. It checks if the trait is directly connected to the class or if any of the class's ancestors have the given trait or if the class or class ancestors' traits uses the checked trait (e.g. it checks the trait hierarchy). It also caches the trait hierarchy as it traverses the tree so subsequent runs with classes that have those traits will perform faster and not have to traverse all or part of the hierarchy.

Here is an example of usage:

use Jimbo2150\PhpUtilities\Traitable;

trait resourceful {

}

trait streamable {
	// ...
}

trait writeable {
	use resourceful;
	use streamable;
	// ...
}

trait readable {
	use resourceful;
	use streamable;
	// ...
}

trait blankable {

}

class StringBuffer {
	use Traitable;
	use writeable;
	use readable;
	// ...
}

$string_stream = new StringBuffer();
$string_stream->hasTrait(resourceful::class); // true
$string_stream->hasTrait(readable::class); // true
$string_stream->hasTrait(blankable::class); // false

Traits

This helper class can be used instead of the Traitable trait to check if a class is an instance of a given trait. Using the same class/trait definition as above, you can run:

use Jimbo2150\PhpUtilities\Traits;

$string_stream = new StringBuffer();
Traits::instanceOf($string_stream, resourceful::class); // true
Traits::instanceOf($string_stream, readable::class); // true
Traits::instanceOf($string_stream, blankable::class); // false

Features

Currently this library has a Traitable trait which allows you to check if the instance has an assigned trait to itself or any of it's ancestors (or any of the trait's traits). It's the equivalent of doing $x instanceof TraitX to check if an object is an instance of a given trait. There is also an associated Trait helper class with static functions which actually perform the trait check.

Feel free to suggest other utilities to add and I will take them under consideration - or submit a pull request with a new utility you would like to have added.

Changelog

See CHANGELOG.md.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2025-03-01