michaelhall / nice-dump
v1.1.0
2018-12-27 17:24 UTC
Requires
- php: >=7.1
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2018-12-29 23:13:36 UTC
README
根据 NiceDump 格式规范 导出 PHP 变量。
需求
- PHP >= 7.1
使用 composer 安装
$ composer require michaelhall/nice-dump
基本使用
创建 NiceDump
<?php require __DIR__ . '/vendor/autoload.php'; // $var can be anything. $var = 'Foo'; // Create a plain dump. $niceDump = \MichaelHall\NiceDump\NiceDump::create($var); // Create a dump with a name. $niceDump = \MichaelHall\NiceDump\NiceDump::create($var, 'var'); // Create a dump with a name and a comment. $niceDump = \MichaelHall\NiceDump\NiceDump::create($var, 'var', 'This is my variable');
输出 NiceDump
<?php require __DIR__ . '/vendor/autoload.php'; // $var can be anything. $var = 'Foo'; // Create a plain dump. $niceDump = \MichaelHall\NiceDump\NiceDump::create($var); // Outputs the NiceDump as: // // =====BEGIN NICE-DUMP===== // eyJ0eXBlIjoic3RyaW5nIiwidmFsdWUiOiJGb28iLCJzaXplIjozfQ== // =====END NICE-DUMP===== echo $niceDump; // Get the output explicitly as a string. $dumpStr = $niceDump->__toString(); // Outputs the same as above. echo $dumpStr;
自定义序列化
通过实现 NiceDumpSerializable
接口和 niceDumpSerialize()
方法,可以为类自定义 NiceDump 序列化。
<?php require __DIR__ . '/vendor/autoload.php'; class Foo implements \MichaelHall\NiceDump\NiceDumpSerializable { public function __construct(string $text) { $this->text = $text; } public function niceDumpSerialize(): array { return [ 'type' => '_text_', 'value' => $this->text, ]; } private $text; } $foo = new Foo('Bar'); $niceDump = \MichaelHall\NiceDump\NiceDump::create($foo); // Outputs the NiceDump as: // // =====BEGIN NICE-DUMP===== // eyJ0eXBlIjoiX3RleHRfIiwidmFsdWUiOiJCYXIifQ== // =====END NICE-DUMP===== // // which corresponds to: // // {"type":"_text_","value":"Bar"} echo $niceDump;
许可
MIT