Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Varexporter Laravel Package

brick/varexporter

Prettier, safer alternative to PHP var_export(). Exports variables as standalone executable PHP code (no runtime dependency) with short array syntax, cleaner numeric arrays, and support for closures and custom objects beyond __set_state(), ideal for fast OPcache-friendly caching.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require brick/varexporter. The main entry point is the VarExporter::export() method, which replaces PHP’s native var_export() and now requires PHP 8.2+. It can serialize and export closures, objects, scalar types, and enums—including stdClass, custom objects, and non-serializable resources (like closures)—without requiring __set_state() or Serializable.

For basic usage:

use Brick\VarExporter\VarExporter;

$exported = VarExporter::export(fn() => 'Hello, World!');
// Returns: 'function () { return "Hello, World!"; }'

$obj = (object) ['name' => 'Alice', 'active' => true];
$exportedObj = VarExporter::export($obj);
// Returns: '(object) ["name" => "Alice", "active" => true]'

// New: Export enums inline
enum Status { case Active; case Inactive; }
$exportedEnum = VarExporter::export(Status::Active, [
    VarExporter::INLINE_LITERAL_LIST
]);
// Returns: 'Status::Active'

Check the README for updated examples and API reference.


Implementation Patterns

  • Config caching: Export configuration arrays, service definitions, or enum-based constants for runtime caching (e.g., in Laravel’s config/cache), avoiding runtime reflection or eval().
  • Test fixtures: Generate reusable, human-readable test fixtures by exporting complex data structures, including enums and closures, instead of hard-coding them.
  • CLI tools & scaffolding: Use VarExporter::export() in artisan commands or dev tools to scaffold code (e.g., generate enum lists, config stubs, or migration templates). Leverage INLINE_LITERAL_LIST for cleaner enum exports.
  • Lazy loading libraries: Integrate with lazy-value wrappers where deferred serialization is needed—e.g., export partial state for deferred reconstruction without Serializable. Now supports enums in lazy-loaded contexts.
  • Integration with frameworks: In Laravel, use it to export routes, middleware stacks, or service bindings for analysis or debugging (e.g., in a custom php artisan debug:config command). Export enum-based statuses or configurations inline for clarity.
  • Enum handling: Export enums as literals (e.g., Status::Active) using INLINE_LITERAL_LIST for cleaner, more maintainable code generation.

Gotchas and Tips

  • PHP 8.2 requirement: Ensure your project meets the minimum PHP version (8.2+) to use this release. Update your php: constraint in composer.json if needed.
  • Dependencies: While it avoids eval(), exported closures retain references to their scope—ensure no dangerous dependencies (e.g., PDO connections) are captured. Consider cleaning scope or using use()-clauses deliberately.
  • Performance: Exporting deeply nested or circular structures can be CPU-intensive. Use VarExporter::exportWithReferences() cautiously—enable reference tracking only when needed (objects/arrays reused multiple times).
  • Readability vs. compactness: The default export format is human-friendly, but not minified. For production config caching, combine with preg_replace or trim if file size matters. Use INLINE_LITERAL_LIST for enums to balance readability and compactness.
  • Custom objects: Non-__set_state objects are exported as (object) [...] but won’t reconstruct as the original class. Use Serializer::serialize() (if available) or custom __set_state() implementations if strict type fidelity is required.
  • Enums: The new INLINE_LITERAL_LIST option exports enums as literals (e.g., Status::Active), but this may not work for all enum use cases (e.g., enums with custom backends). Test thoroughly in your context.
  • Customization hooks: Extend via VarExporter::export()’s optional $options parameter (e.g., VarExporter::export($data, [VarExporter::INLINE_LITERAL_LIST])). Check the source for extensibility points if you need custom handling for enums or other types.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport