PHPackages                             aza/phpgen - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. aza/phpgen

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

aza/phpgen
==========

AzaPhpGen - Anizoptera CMF PHP code generation (dump) component. Allows to dump complex arrays, objects, closures and basic data types as php code. In part, this can be called a some sort of serialization. You can customize your dumped php code as you wish.

v1.1.1(12y ago)113.4k2MITPHPPHP &gt;=5.3.3

Since Feb 26Pushed 12y ago1 watchersCompare

[ Source](https://github.com/Anizoptera/AzaPhpGen)[ Packagist](https://packagist.org/packages/aza/phpgen)[ Docs](https://github.com/Anizoptera/AzaPhpGen)[ RSS](/packages/aza-phpgen/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (2)

AzaPhpGen
=========

[](#azaphpgen)

Anizoptera CMF PHP code generation (dump, serialization) component.

[![Build Status](https://camo.githubusercontent.com/8ef30d26c02bfa879892abf54c637c8366b44d07ebf2d11c6c279d86facadc1f/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f416e697a6f70746572612f417a6150687047656e2e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/Anizoptera/AzaPhpGen)

Table of Contents
-----------------

[](#table-of-contents)

1. [Introduction](#introduction)
2. [Requirements](#requirements)
3. [Installation](#installation)
4. [Examples](#examples)
    - [Simple dump](#example-1---simple-dump)
    - [Array dump](#example-2---array-dump)
    - [Traversable dump](#example-3---traversable-dump)
    - [Closure (anonymous function) example](#example-4---closure-anonymous-function-example)
    - [Custom object dumping with IPhpGenerable interface](#example-5---custom-object-dumping-with-iphpgenerable-interface)
    - [Bundled CustomCode class usage](#example-6---bundled-customcode-class-usage)
    - [Custom object dumping with defined handlers](#example-7---custom-object-dumping-with-defined-handlers)
    - [AzaPhpGen customization](#example-8---azaphpgen-customization)
5. [Tests](#tests)
6. [Credits](#credits)
7. [License](#license)
8. [Links](#links)

Introduction
------------

[](#introduction)

Allows to dump complex arrays, objects, closures and basic data types as php code. In part, this can be called a some sort of serialization. And you can customize your dumped php code as you wish.

It is very usefull for code compilation (usually for caching purposes).

**Features:**

- Supports all scalar values (bool, int, float, string), nulls, arrays, serializable objects;
- [Traversable](http://php.net/traversable) support (dumped as array, see usage in [Example #3](#example-3---traversable-dump));
- Closures support (closures with "use", several closures on the same line are not supported!) (see usage and more info in [Example #4](#example-4---closure-anonymous-function-example));
- Custom object dumping with [IPhpGenerable interface](IPhpGenerable.php) (see usage in [Example #5](#example-5---custom-object-dumping-with-iphpgenerable-interface));
- Bundled simple [CustomCode class](CustomCode.php) (see usage in [Example #6](#example-6---bundled-customcode-class-usage));
- Custom object dumping with defined handlers/hooks (see usage in [Example #7](#example-7---custom-object-dumping-with-defined-handlers));
- Very flexible configuration (9 code building options, see in [PhpGen class code](PhpGen.php#L19));
- Automatic recognition of binary strings;
- Convenient, fully documented and test covered API;

**Benefits over `var_export()`:**

- `var_export` does not support Closures dumping;
- `var_export` supports only objects with `__set_state` function. AzaPhpGen supports all serializable objects;
- AzaPhpGen dumps Traversable objects as arrays (via `iterator_to_array`);
- For binary strings `var_export` generates very ugly code that is awkward to use and can be easily corrupted;
- For objects `var_export` generates code that can not be evaluated in namespace;
- AzaPhpGen give you full control over objects dumping with custom handlers and `IPhpGenerable` interface;
- With AzaPhpGen you can flexibly customize formatting of your code (useful for arrays);
- AzaPhpGen can generate code with or without trailing semicolon. `var_export` never outputs it :)
- Some detailed comparisons you can see in [Tests/PhpGenBenchmarkTest.php](Tests/PhpGenBenchmarkTest.php#L647);

Requirements
------------

[](#requirements)

- PHP 5.3.3 (or later);
- SPL and Reflection extensions for closures support (both bundled with PHP by default);

Installation
------------

[](#installation)

The recommended way to install AzaPhpGen is [through composer](http://getcomposer.org). You can see [package information on Packagist](https://packagist.org/packages/aza/phpgen).

```
{
	"require": {
		"aza/phpgen": "~1.0"
	}
}
```

Examples
--------

[](#examples)

You can use [examples/example.php](examples/example.php) to run all examples.

#### Example #1 - Simple dump

[](#example-1---simple-dump)

```
// Get singleton instance of PhpGen (fast and simple variant)
$phpGen = PhpGen::instance();
// Integer
echo $phpGen->getCode(123456789) . PHP_EOL; // 123456789;
// String (binary strings are supported as well)
echo $phpGen->getCode('some string' . ' example') . PHP_EOL; // "some string example";
// Float without trailing semicolon
echo $phpGen->getCodeNoTail(12.345) . PHP_EOL; // 12.345
// Simple serializable objects
$var = new stdClass();
echo $phpGen->getCode($var) . PHP_EOL; // unserialize("O:8:\"stdClass\":0:{}");
// Another object example
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($var) . PHP_EOL; // unserialize("O:8:\"DateTime\":3:{s:4:\"date\";s:19:\"2013-02-23 00:49:36\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:3:\"UTC\";}");
```

#### Example #2 - Array dump

[](#example-2---array-dump)

```
// AzaPhpGen will use short array syntax if possible by default (PHP >= 5.4)
echo $phpGen->getCode(array(
	true, false, null
)) . PHP_EOL;
/*
[
	true,
	false,
	null,
];
 */

// Build code without formatting
echo $phpGen->getCodeNoFormat(array(
	true, false, null
)) . PHP_EOL;
/*
[true,false,null];
 */

// Complex array (some sort of config for example)
$array = array(
	'key1'     => 'value',
	'long_key' => 'value',
	'array'    => array(
		'short_value'
	),
	'array2' => array(
		'very very very very very very very very very very very very long value'
	),
	'other',
	123456789
);
echo $phpGen->getCode($array) . PHP_EOL;
/*
[
	"key1"     => "value",
	"long_key" => "value",
	"array"    => ["short_value"],
	"array2"   => [
		"very very very very very very very very very very very very long value",
	],
	0 => "other",
	1 => 123456789,
];
 */

// And wothout formatting
echo $phpGen->getCodeNoFormat($array) . PHP_EOL;
/*
["key1"=>"value","long_key"=>"value","array"=>["short_value"],"array2"=>["very very very very very very very very very very very very long value"],0=>"other",1=>123456789];
 */
```

#### Example #3 - Traversable dump

[](#example-3---traversable-dump)

AzaPhpGen treat all Traversable objects as arrays (with [iterator\_to\_array](http://php.net/iterator-to-array)).

```
$var = new SplFixedArray(3);
$var[0] = 'a';
$var[1] = 'b';
echo $phpGen->getCodeNoFormat($var) . PHP_EOL; // ["a","b",null];
```

#### Example #4 - Closure (anonymous function) example

[](#example-4---closure-anonymous-function-example)

**WARNING:** Closures are dumped as is. So complex closures are not supported:

- Closures with "use" statement (closures that inherit variables from the parent scope);
- Several closures on the same line;
- Usage of non-qualified class name (with importing) in closure;
- Closures with `$this` variable usage;

```
$closure = function($a, $b) {
	return round($a, $b) . "example\t\n";
};
echo $phpGen->getCode($closure) . PHP_EOL;
/*
function($a, $b) {
	return round($a, $b) . "example\t\n";
};
 */
echo $phpGen->getCode(array('key' => $closure)) . PHP_EOL;
/*
[
	"key" => function($a, $b) {
	return round($a, $b) . "example\t\n";
},
];
 */
```

#### Example #5 - Custom object dumping with IPhpGenerable interface

[](#example-5---custom-object-dumping-with-iphpgenerable-interface)

You can customize dumping of your classes by implementing the `IPhpGenerable` interface.

```
class ExampleCustomCode implements IPhpGenerable
{
	public function generateCode()
	{
		return '32434 + 5678';
	}
}

$var = new ExampleCustomCode();

echo $phpGen->getCode($var) . PHP_EOL; // 32434 + 5678;

echo $phpGen->getCode(array($var)) . PHP_EOL; // [32434 + 5678];
```

#### Example #6 - Bundled CustomCode class usage

[](#example-6---bundled-customcode-class-usage)

For the simpliest varint of `IPhpGenerable` interface usages you can use bundled class - `CustomCode`. It just takes the required code as a constructor argument.

```
$var = new CustomCode('"some code" . PHP_EOL');

echo $phpGen->getCode($var) . PHP_EOL; // "some code" . PHP_EOL;

echo $phpGen->getCode(array($var)) . PHP_EOL; // ["some code" . PHP_EOL];
```

#### Example #7 - Custom object dumping with defined handlers

[](#example-7---custom-object-dumping-with-defined-handlers)

Second varint of resulting code customization - usage of defined handlers (hooks) for the classes. This way you can customize dump of any possible class!

```
// Set custom handler for DateTime type
$phpGen->addCustomHandler('DateTime', function($data) use ($phpGen) {
	/** @var $data \DateTime */
	return $phpGen->getCodeNoTail(
		$data->format("Y-m-dO")
	);
});
// Build code
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($var) . PHP_EOL; // "2013-02-23+0000";
```

#### Example #8 - AzaPhpGen customization

[](#example-8---azaphpgen-customization)

AzaPhpGen has many options. So it's very simple to configure your resulting code for your special needs (code style for example). You can see all available options in the [PhpGen class code](PhpGen.php#L19).

```
// Disable short array syntax and use 6 spaces for indentation
$phpGen->shortArraySyntax = false;
$phpGen->useSpaces        = true;
$phpGen->tabLength        = 6;
$var = array(array(array(23 => 'example')));
echo $phpGen->getCode($var) . PHP_EOL;
/*
array(
      array(
            array(
                  23 => "example",
            ),
      ),
);
 */
```

Tests
-----

[](#tests)

Tests are in the `Tests` folder and reach 100% code-coverage. To run them, you need PHPUnit. Example:

```
$ phpunit --configuration phpunit.xml.dist

```

Or with coverage report:

```
$ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/

```

Credits
-------

[](#credits)

AzaPhpGen is a part of [Anizoptera CMF](https://github.com/Anizoptera), written by [Amal Samally](http://azagroup.ru/about/#amal) (amal.samally at gmail.com) and [AzaGroup](http://azagroup.ru/) team.

License
-------

[](#license)

Released under the [MIT](LICENSE.md) license.

Links
-----

[](#links)

- [Composer package](https://packagist.org/packages/aza/phpgen)
- [Last build on the Travis CI](http://travis-ci.org/Anizoptera/AzaPhpGen)
- [Project profile on the Ohloh](https://www.ohloh.net/p/AzaPhpGen)
- Other Anizoptera CMF components on the [GitHub](https://github.com/Anizoptera) / [Packagist](https://packagist.org/packages/aza)
- (RU) [AzaGroup team blog](http://azagroup.ru/)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Total

3

Last Release

4738d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/356b06126f37416ba259cc00868ae0960bad500f459186a25a7cc3b90c80fcb5?d=identicon)[amal](/maintainers/amal)

---

Top Contributors

[![art-shen](https://avatars.githubusercontent.com/u/423417?v=4)](https://github.com/art-shen "art-shen (35 commits)")

---

Tags

anizoptera-cmfarray-dumpcode-generationcomposerpackagistphpphp-libraryserializertravisphpcodedumpserializationgeneration

### Embed Badge

![Health badge](/badges/aza-phpgen/health.svg)

```
[![Health](https://phpackages.com/badges/aza-phpgen/health.svg)](https://phpackages.com/packages/aza-phpgen)
```

###  Alternatives

[jbzoo/jbdump

Script for debug and dump PHP variables and other stuff. This tool is a nice replacement for print\_r() and var\_dump() functions.

211.1M3](/packages/jbzoo-jbdump)[php-sage/sage

☯ Insightful PHP debugging assistant.

5639.7k5](/packages/php-sage-sage)[bavix/laravel-xhprof

Quick profiling of your code for Laravel

22156.6k](/packages/bavix-laravel-xhprof)[h4cc/phpqatools

A meta composer package for PHP QA Tools.

6418.6k1](/packages/h4cc-phpqatools)[thehocinesaad/laravel-error-ai

This package adds Ask AI button to the error page.

2214.4k](/packages/thehocinesaad-laravel-error-ai)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
