ntlab/php-obj

将 PHP 对象表示为字符串、JavaScript、注释或 YAML

v1.8.0 2024-09-25 02:15 UTC

This package is auto-updated.

Last update: 2024-09-25 02:16:28 UTC


README

Build Status Latest Stable Version Total Downloads License

将 PHP 对象表示为字符串、JavaScript、注释或 YAML。

示例

  • 将 PHP 对象表示为字符串

    <?php
    
    use NTLAB\Object\PHP;
    
    $a = new PHP([1, 2, 3], ['inline' => true]);
    echo (string) $a; // [1, 2, 3]
    
    $a = new PHP(['name' => 'Apple', 'color' => 'Red', 'description' => 'It\'s yummy...'], ['inline' => true]);
    echo (string) $a; // ['name' => 'Apple', 'color' => 'Red', 'description' => 'It\'s yummy...']
  • 将 PHP 对象表示为注释

    <?php
    
    use NTLAB\Object\Annotation;
    
    $a = new Annotation(['name' => 'Apple', 'color' => 'Red', 'description' => 'It\'s yummy...'], ['annotation' => '@Fruit', 'inline' => true]);
    echo (string) $a; // @Fruit(name="Apple", color="Red", description="It's yummy...")
  • 将 PHP 对象表示为 JavaScript

    <?php
    
    use NTLAB\Object\JS;
    
    $a = new JS(['name' => 'Apple', 'color' => 'Red', 'description' => 'It\'s yummy...'], ['inline' => true]);
    echo (string) $a; // {name: 'Apple', color: 'Red', description: 'It\'s yummy...'}
    • 将 PHP 对象表示为 YAML
    <?php
    
    use NTLAB\Object\YAML;
    
    $a = new YAML(['name' => 'Apple', 'color' => 'Red', 'description' => 'It\'s yummy...']);
    echo (string) $a;
    // name:
    //     Apple
    // color:
    //     Red
    // description:
    //     It's yummy...