berbeflo/modify-dump 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

berbeflo/modify-dump

Composer 安装命令:

composer require berbeflo/modify-dump

包简介

A small library that makes use of php8 attributes to modify the var_dump output of objects

README 文档

README

1. Modify Dump

Enables easy modification of var_dump output with Attributes, that were introduced in PHP 8.

1.1. Motivation

I just wanted to play around with Attributes and this library was the first use-case that came into mind.

1.2. How it works

PHP has a bunch of so called magic methods. These are object (or class) methods that get called under certain circumstances. Most of the time you'll come into touch with __construct(), which is called on object instantiation.
One of the less known magic methods is __debugInfo() which is called on calling var_dump() on an object.
This library provides a Trait that overrides __debugInfo() and makes some magic based on the Attributes for the class and it's properties.

1.3. Usage

1.3.1. Installation

composer require berbeflo/modify-dump

1.3.2. First example

Use the Trait ModifiedDump and tell it which properties to show:

<?php

declare(strict_types=1);

use Berbeflo\ModifyDump\Attribute\Dump;
use Berbeflo\ModifyDump\Trait\ModifiedDump;

require_once('vendor/autoload.php');

class Test
{
    use ModifiedDump;

    #[Dump]
    private string $a = "test";
    protected int $b;
    #[Dump]
    public int | string $c = 5;
}

var_dump(new Test());

The corresponding output:

object(Test)#3 (3) {
  ["a"]=>
  string(4) "test"
  ["b"]=>
  object(Berbeflo\ModifyDump\State\Uninitialized)#9 (0) {
  }
  ["c"]=>
  int(5)
}

1.3.3. Further examples

Have a look at the examples folder!

1.4. Features

1.4.1. Filters

Filters allow to exclude properties, e.g. private properties without the burden to mark every property that should not be returned.
The default filter excludes all properties that have no Dump Attribute.

1.4.2. Formatters

Formatters allow to modify the displayed content of a property.

1.4.3. Statistics

Statistics allow to add class/object statistics to the output.

1.4.4. Extendibility

It's easy to add new Formatters, Filters and Statistics.

1.4.5. Not restricted to var_dump

While the delivered trait overrides __debugInfo(), the logic is decoupled from this method. So it'd be easy to use this library to e.g. write a simple toArray() method.

2. Navigation

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固