selfiens/var-export

PHP value serializer

v1.0.0 2025-05-16 03:43 UTC

This package is auto-updated.

Last update: 2025-06-16 03:59:32 UTC


README

PHP VarExport is a lightweight PHP library for serializing variables into a string representation of PHP code. It produces output that mimics modern PHP syntax, particularly short array notation ([]). This makes it a useful alternative to PHP's native var_export() or json_encode() when you need human-readable PHP code that can be directly embedded or evaluated.

Key Features

  • Modern PHP Syntax: Outputs arrays using short array syntax ([]).
  • Two Output Styles:
    • compact(): Generates a concise, single-line representation.
    • formatted(): Produces a human-readable, indented representation.
  • Comprehensive Type Handling: Supports various data types including:
    • Arrays (both list and associative)
    • Scalars (strings, integers, floats, booleans, null)
    • Objects (converts to an array via a public toArray() method or public properties)
    • Resources (represented as a descriptive string)
    • Callables (including Closures, represented as descriptive strings)
  • Recursion Depth Control: Prevents infinite loops and excessive memory usage with a configurable maximum depth.
  • Precise Float Formatting: Custom float-to-string conversion (floatToString()) aims to avoid scientific E-notation and ensures whole floats are represented with .0 (e.g., 1.0 instead of 1).
  • Informative Comments: Includes comments for object class names and callables (in formatted mode) for better readability.

Requirements

  • PHP 8.1 or higher

Installation

Install the library using Composer:

composer require selfiens/var-export

Usage

Import the class and use its static methods:

<?php

require 'vendor/autoload.php';

use Selfiens\VarExport\VarExport;

// Example data
$data = [
    'message' => 'Hello, World!',
    'count' => 42,
    'is_active' => true,
    'nested_list' => [10, 20, 30],
    'a_float' => 0.0000007,
    'null_value' => null,
    'an_object' => (object)['property' => 'value'],
];

// --- Compact Output ---
echo VarExport::compact($data);
// ['message'=>'Hello, World!','count'=>42,'is_active'=>true,'nested_list'=>[10,20,30],'a_float'=>0.0000007,'null_value'=>NULL,'an_object'=>/* stdClass */['property'=>'value']]

// --- Formatted Output ---
echo VarExport::formatted($data);
// [
//     'message' => 'Hello, World!',
//     'count' => 42,
//     'is_active' => true,
//     'nested_list' => [
//         10,
//         20,
//         30,
//     ],
//     'a_float' => 0.0000007000,
//     'null_value' => NULL,
//     'an_object' => /* stdClass */[
//         'property' => 'value',
//     ],
// ]

// --- Handling Objects with toArray() method ---
class MyCustomObject
{
    private string $secret = 'hidden';
    public string $name = 'My Object';
    public int $id = 123;

    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'id' => $this->id,
            'type' => 'custom_export'
        ];
    }
}

$customObject = new MyCustomObject();
echo VarExport::formatted($customObject);
// /* MyCustomObject */[ // Actual namespace will be shown
//     'name' => 'My Object',
//     'id' => 123,
//     'type' => 'custom_export',
// ]


// --- Custom Indentation for Formatted Output ---
echo VarExport::formatted($data, "    "); // Use 4 spaces for indentation
// Output will be indented with 4 spaces instead of tabs

Method Signatures

public static function compact(mixed $var, int $max_depth = 20, int $current_depth = 0): string;

public static function formatted(mixed $var, string $tab = "\t", int $max_depth = 20, int $current_depth = 0): string;
  • $var: The variable to export.
  • $max_depth (optional): The maximum recursion depth. Defaults to 20.
  • $tab (optional, for formatted only): The string to use for indentation. Defaults to \t (tab).
  • $current_depth (internal use): Tracks the current recursion depth.